【问题标题】:How to check if the user has clicked a button in Selenium?如何检查用户是否单击了 Selenium 中的按钮?
【发布时间】:2018-05-31 04:24:41
【问题描述】:

我正在抓取一个带有验证码表单的网站。用户通过选择框选择选项,手动输入验证码并点击Go按钮(请参考截图)。

现在,一旦用户点击了提交按钮,我想通过 Selenium Webdriver 知道。

以下是 Python 中的部分代码:

url= 'http://services.ecourts.gov.in/ecourtindia/cases/s_order.php?state=D&state_cd=26&dist_cd=9'

driver=webdriver.Chrome()
driver = webdriver.Chrome()
driver.get(url)

try:
    time.sleep(10)
    element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, "submit1")))
except Exception as e:
    print str(e)

【问题讨论】:

  • 不,没有重定向。提交后,提交按钮下方会显示一个表格,其中包含数据(通常需要几秒钟才能加载)

标签: python selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

一旦您单击提交按钮,我就假设(这很有可能)发生了重定向。

点击提交按钮后,您将被重定向到一个新页面,该页面将包含一些内容,对吗?

你必须在这里做什么:

element = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((Locator, "value"))).text  

并且您已经知道 value ,这将是预期值。

现在您可以断言这两个值。

更新

正如你所提到的,出现了一个表格,现在你可以在这里做的是:

等待表格的可见性并使用 .text 获取表格的标题并声明该值。

【讨论】:

  • 它不会重定向到新页面。它在验证后将数据加载到同一页面上,这通常需要几秒钟)。这是链接:services.ecourts.gov.in/ecourtindia/cases/…
  • WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'showList')))
  • 之后你可以使用:case = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((BY.XPATH,'//th[text()='Case Type/Case Number/ Case Year']'))).text 获取表格第二列的文本,注意你已经知道了文本,这里以后可以断言。
  • 非常感谢,@cruisepandey
  • @cruisepandey 根据文档visibility_of_element_located(locator) can't accept 2 arguments as in EC.visibility_of_element_located((locator of new page after redirection, "value"))
【解决方案2】:

前段时间我遇到过类似的问题,但在我的情况下,页面可以加载 2 个不同的模板而无需重新加载,有时会显示一个表格,有时会显示一个模式,告诉类似“没有数据”之类的内容

所以我无法搜索将出现在页面上的新元素,我的解决方案是每当用户单击按钮时在页面上插入我自己的元素(通过 javascript),然后搜索这个新元素。

这是一个例子:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By

# first you need to get your button at python to inject the javascript
element = driver.find_element_by_xpath("element_xpath")


# here is a sample of an element you can create via js (this one is a hidden input)
javascript = "let element = arguments[0];\
              element.addEventListener('click', function() { \
                    let input = document.createElement('input'); \
                    input.setAttribute('type', 'hidden');  \
                    input.setAttribute('id', 'my_input'); \
                    document.body.appendChild(input); \
              });"
                            
        
# finally you send the javascript to your webbrowser with execute_script
driver.execute_script(javascript,element)

# then you will be able to wait the element to be created 
#(30 is te timeout in seconds, you should adjust as you need)
hiddenInput = WebDriverWait(driver, 30).until(EC.presence_of_element_located(
            (By.XPATH, '//*[@id="my_input"]')
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2014-04-09
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多