【问题标题】:Expected_Conditions returns TimeoutException while using element_to_be_clickable method through selenium & python 3通过 selenium 和 python 3 使用 element_to_be_clickable 方法时,Expected_Conditions 返回 TimeoutException
【发布时间】:2018-05-09 15:54:13
【问题描述】:

我正在尝试使用 selenium 和 python 3 实现自动化,以下是我尝试过的代码:

element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//*[@id="download"]/span'))
)
element.click()

HTML:

<a href="javascript:void(0);" 
onclick="if(!alen_Button_checkClick('download',event)){return true;};return 
htmlbSL(this,2,'download:downloadBtn')" 
onkeypress="if(!alen_Button_checkClick('download',event)){return 
true;};return htmlbSL(this,2,'download:downloadBtn')" class="urBtnStd1 urV" 
id="download" ct="Button" title="Download To Excel" style="white- 
space:nowrap;">
    <span class="urBtnPadding">
        Download To Excel
    </span>
</a>

我想单击“下载到 Excel 按钮”,但每次执行代码时,无论是通过 xpath 还是类搜索,它都不会执行,并且当我增加超时时会出现超时错误,它没有帮助或者我使用 @ 987654323@that 也无济于事。

堆栈跟踪:

EC.element_to_be_clickable((By.XPATH, '//*[@id="download"]/span'))
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
  selenium.common.exceptions.TimeoutException: Message

我在这里错过了什么?

编辑 2:

time.sleep(5)
if driver.find_element_by_id('download'):
    print ("Element exists")
time.sleep(5)
if driver.find_element_by_xpath((("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))):
    print ("Element exists1")

编辑 2 的堆栈跟踪:

Element exists
Traceback (most recent call last):
  File "C:\Users\c5242046\Desktop\test2\backupautomation\bkpalertv1\bkpv1.py", line 73, in <module>
    if driver.find_element_by_xpath((("//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))):
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
    'value': value})['value']
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "c:\program files (x86)\python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with xpath == //a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']

【问题讨论】:

  • 您是否尝试通过 id='download' 查找元素来单击? driver.find_element_by_id('download').click() 之类的东西。如果没有任何效果,您可以随时使用 JavaScript 点击“下载”链接
  • 我试过driver.find_element_by_id('download').click() 但它不起作用,也没有输出,程序运行没有错误。如何使用 Javascript 点击下载链接?我不知道
  • 如果您使用的是 IE,请按 F12 并移动到控制台窗口并输入 document.getElementById('download').click(); 。检查它是否点击了链接。
  • 是的,它确实点击了
  • 那就试试这个吧。 driver.execute_script("document.getElementById('download').click();"); 。我不确定 Python 的语法。我想它应该工作。如果它不起作用,请更正execute_script 语法。

标签: python selenium-webdriver


【解决方案1】:

要单击文本为 Download To Excel 的元素,您必须诱导 WebDriverWait 并且可以使用以下任一选项:

  • CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.urBtnStd1.urV#download[title='Download To Excel']>span.urBtnPadding"))).click()
    
  • XPATH

    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='urBtnStd1 urV' and @id='download' and @title='Download To Excel']/span[@class='urBtnPadding']"))).click()
    

【讨论】:

  • 两个都试过了,还是一样的错误。查看我所做的编辑
  • @Alisa TimeoutExceptionfailed expected-conditions 的结果。通过find_element_by_*time.sleep() 结合使用来调试您的代码。如果您能够找到元素并使用观察更新问题。
  • 我使用的浏览器是IE
  • 它无法通过 xpath 或 class 定位,而是通过 id。
  • @Alisa 尝试使用我在答案中提供的 xpath 或 css 来定位元素
猜你喜欢
  • 2020-11-26
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多