【发布时间】: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语法。