【问题标题】:How to click on a button inside a side bar label with Selenium and Python如何使用 Selenium 和 Python 单击侧栏标签内的按钮
【发布时间】:2020-12-12 10:34:19
【问题描述】:

我正在尝试单击侧边栏标签内的按钮:

<span class="sidebar-label">
    Exportar Listagem
</span>

这是我的代码:

driver.get("https://ispot2.faturaiqos.pt")
html = driver.page_source
exp = driver.find_element_by_link_text("Exportar Listagem")
exp.click()

这是我得到的错误:

Traceback (most recent call last):
File "/home/pi/Desktop/Python Testes/Ispot JP.py", line 25, in <module>
exp = driver.find_element_by_link_text("Exportar Listagem")
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 317, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 745, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Exportar Listagem"}
(Session info: chrome=84.0.4147.141)
(Driver info: chromedriver=84.0.4147.141 (80c974bf7990b9735a8e885046fc5c9b1da4796c-refs/branch-heads/4147@{#1132}),platform=Linux 5.4.79-v7+ armv7l)

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver xpath webdriverwait


    【解决方案1】:

    要单击文本为 Exportar Listagem 的元素,您可以使用以下Locator Strategies

    • 使用 xpathcontains():

      driver.find_element_by_xpath("//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]").click()
      
    • 使用 xpathnormalize-space():

      driver.find_element_by_xpath("//span[@class='sidebar-label' and normalize-space()='Exportar Listagem']").click()
      

    理想情况下,点击您需要为WebDriverWait 诱导element_to_be_clickable() 的元素,您可以使用以下任一Locator Strategies

    • 使用 xpathcontains():

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]"))).click()
      
    • 使用 xpathnormalize-space():

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sidebar-label' and normalize-space()='Exportar Listagem']"))).click()
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 完美 :),我的下一个问题是:我到了这个“Exportar Listagem”页面,现在我需要填写 2 个日期表格,然后单击按钮“Exportar”日期来自 日期到 点击按钮:Exportar
    • @PeterPan 听起来是一个不同的问题。你能根据你的新要求提出一个新问题吗? Stackoverflow 贡献者将很乐意为您提供帮助。
    • @PeterPan 很高兴能为您提供帮助。 Vote up questions and answers 您发现这对未来读者的利益很有帮助。见Why is voting important
    猜你喜欢
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2018-08-22
    • 2023-03-16
    • 2021-04-19
    • 1970-01-01
    • 2019-09-01
    • 2022-01-07
    相关资源
    最近更新 更多