【问题标题】:Unable to click on link using selenium and getting an error无法使用 selenium 单击链接并出现错误
【发布时间】:2021-06-04 18:40:15
【问题描述】:

这是我的问题:

我无法点击输入类型按钮(屏幕截图 - 指示器 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)

【问题讨论】:

    标签: html python-3.x selenium anaconda selenium-chromedriver


    【解决方案1】:

    试试这个:

    export_btn = driver.find_element_by_xpath("//*[contains(@id, 'BodyContent_BodyContent_FilterUsersControl_UserGrid_']")
    

    您正在尝试的那个看起来是动态 ID,我已尝试从中提取静态数据。

    还要确保它不应该在任何iframe 中。

    【讨论】:

    • 我得到这个错误:selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//input[ @type="button" and @title="Export to Excel"]"} (Session info: chrome=91.0.4472.77) 我有办法把源代码发给你吗,因为这个 HTML 文件有 663 行代码。
    • 你检查过 iframe 吗?它在里面吗?
    • 是的,但我该如何确定 iframe 所在的索引...我想我只看到一个。 iframes = driver.find_elements_by_xpath("//iframe") 然后使用 iframes .get_attribute("src") 因为我的是这个
    • 直接使用css选择器切换iframe[src=' src path']
    【解决方案2】:

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

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

    【讨论】:

    • 我得到这个错误:selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//input[ @type="button" 和 @title="Export to Excel"]"}(会话信息:chrome=91.0.4472.77)
    猜你喜欢
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    相关资源
    最近更新 更多