相关疑难解决方法(0)

如何让Selenium点击可变数量的"下一个"按钮?

我有一个内部Web应用程序,它有一个模态对话框.不幸的是,我不能在这里发布实际的Web应用程序位置,但让我尽可能地描述它.

  • 当应用程序启动时,屏幕上会出现一个框,告诉您一堆文本.您可以按"下一步"以获取下一页文本.
  • 在最后一页上,"下一步"按钮被禁用,并且启用了Web应用程序的其余UI.
  • 页面数量可变,所以我不知道有多少次点击"下一步".

我可以点击固定次数(例如:如果我知道有两个页面我可以点击两次)但我不确定如何改变它以便无论我有多少页面它都会运行.我想要一个普遍的解决方案; 据推测,这会使用某种循环来检查按钮是否已启用.如果是,则单击它.如果它被禁用,则退出循环.

问题是:如何在Selenium中设置循环,重复单击按钮直到它被禁用?

这是我尝试过的代码:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

driver.get("http://localhost/myapp")

try:
    wait = WebDriverWait(driver, 100)    
    wait.until(EC.element_to_be_clickable((By.ID,'menuIntroBox_buttonNext')))    
    driver.find_element_by_id("menuIntroBox_buttonNext").click()

    # Click through the introduction text... this is the problematic code.
    # Here I tried to wait for the element to …
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-webdriver

8
推荐指数
1
解决办法
5327
查看次数

Python Selenium单击下一步按钮直到结束

这是我的第一个问题的后续问题,我正在尝试抓取一个网站并点击Selenium next(直到它无法点击)并收集结果.

这是带有按钮的网站的html标签:

<div class="pager results-display">
  <span class="action-btn prev inactive" data-page="1">Prev</span>
  <span class="action-btn inactive" data-page="1">1</span>  # Currently in page 1 thus inactive
  <span class="action-btn" data-page="2">2</span>
  <span class="action-btn" data-page="3">3</span> 
  <span class="action-btn" data-page="4">4</span>
  <span class="action-btn" data-page="5">5</span>
  <span class="action-btn" data-page="6">6</span>
  <span class="action-btn" data-page="7">7</span>
  <span class="action-btn" data-page="8">8</span> 
  <span class="action-btn" data-page="9">9</span>
  <span class="action-btn" data-page="10">10</span>
  <span class="action-btn" data-page="11">11</span> 
  <span class="action-btn" data-page="12">12</span>
  <span class="action-btn" data-page="13">13</span>
  <span class="action-btn" data-page="14">14</span>
  <span class="action-btn next" data-page="2">Next</span>
</div>
<div class="no-results-display hidden" style="display: none;">
                    No companies matched your …
Run Code Online (Sandbox Code Playgroud)

python selenium webdriver selenium-webdriver

4
推荐指数
1
解决办法
3207
查看次数

标签 统计

python ×2

selenium ×2

selenium-webdriver ×2

webdriver ×1