首页 > 解决方案 > 无法使用 selenium 单击链接并出现错误

问题描述

这是我的问题:

截屏

我无法单击输入类型按钮(屏幕截图 - 指示器 3)来下载文件,我确实看到了 onclick 功能,但不确定为什么它没有单击。如果我使用 action.move_by_offset(1820,215).click().perform() 我可以单击导出,但这只是一个创可贴修复。我创建了一个脚本来单击网页的其他部分(屏幕截图 - 指示器 1),它工作正常。创建脚本以单击此区域(屏幕截图 - 指示器 2)时,我收到错误消息(见下文)。我已经尝试了 driver.find_element_by_xpath、driver.find_element(By.XPATH)、driver.find_element_by_css_selector、driver.find_element(By.CSS_SELECTOR) 以及 webdriverwait,但我没有运气。下面我包含了一段 HTML 代码、我在 python 中使用的代码以及我收到的错误。

HTML:

    <input type="button"        
    name="ctl00$ctl00$BodyContent$BodyContent$FilterUsersControl$UserGrid$ctl00$ctl02$ctl00$ExportToExcelButton"
    value=" " 
    onclick="if(!$find('ctl00_ctl00_BodyContent_BodyContent_FilterUsersControl_UserGrid_ctl00').exportToExcel())
    return false;
    __doPostBack('ctl00$ctl00$BodyContent$BodyContent$FilterUsersControl$UserGrid$ctl00$ctl02$ctl00$ExportToExcelButton','')"
    id="ctl00_ctl00_BodyContent_BodyContent_FilterUsersControl_UserGrid_ctl00_ctl02_ctl00_ExportToExcelButton" 
    title="Export to Excel" class="rgExpXLS">

代码:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
link = 'https://xxxxxx.xxxxxx.com'
driver = webdriver.Chrome(executable_path='//xxxxx/xxxxx/xxxxxxx/Python/Script/chromedriver.exe')
driver.get(link)
time.sleep(20)
action = ActionChains(driver)
export_btn = driver.find_element_by_xpath('//*[@id="ctl00_ctl00_BodyContent_BodyContent_FilterUsersControl_UserGrid_ctl00_ctl02_ctl00_ExportToExcelButton"]')
export_btn.click()

错误:

Traceback (most recent call last):
  File "h:/Python/Script/update_UserList.py", line 42, in <module>
    export_btn = driver.find_element_by_xpath('//*[@id="ctl00_ctl00_BodyContent_BodyContent_FilterUsersControl_UserGrid_ctl00_ctl02_ctl00_ExportToExcelButton"]')
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Anaconda\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="ctl00_ctl00_BodyContent_BodyContent_FilterUsersControl_UserGrid_ctl00_ctl02_ctl00_ExportToExcelButton"]"}
  (Session info: chrome=91.0.4472.77)

标签: htmlpython-3.xseleniumanacondaselenium-chromedriver

解决方案


如果不查看整个页面的源,很难明确地说出,但通常您可能会考虑在 xpath 中使用不同的属性,或者更具体地了解您在 xpath 中查找的节点。例如,这样事情可能不是答案,但它可能会导致更好的问题!


export_btn = driver.find_element_by_xpath('//input[@type="button" and @title="Export to Excel"]')

推荐阅读