如何使用 Selenium 和 Python 单击侧边栏标签内的按钮
How to click on a button inside a side bar label with Selenium and Python
我正在尝试单击侧边栏标签内的按钮:
<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)
要单击文本为 Exportar Listagem 的元素,您可以使用以下 :
使用 xpath 和 contains()
:
driver.find_element_by_xpath("//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]").click()
使用 xpath 和 normalize-space()
:
driver.find_element_by_xpath("//span[@class='sidebar-label' and normalize-space()='Exportar Listagem']").click()
理想情况下,要单击需要为 element_to_be_clickable()
引入 WebDriverWait 的元素,您可以使用以下任一方法 :
使用 xpath 和 contains()
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]"))).click()
使用 xpath 和 normalize-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
我正在尝试单击侧边栏标签内的按钮:
<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)
要单击文本为 Exportar Listagem 的元素,您可以使用以下
使用 xpath 和
contains()
:driver.find_element_by_xpath("//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]").click()
使用 xpath 和
normalize-space()
:driver.find_element_by_xpath("//span[@class='sidebar-label' and normalize-space()='Exportar Listagem']").click()
理想情况下,要单击需要为 element_to_be_clickable()
引入 WebDriverWait 的元素,您可以使用以下任一方法
使用 xpath 和
contains()
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='sidebar-label' and contains(., 'Exportar Listagem')]"))).click()
使用 xpath 和
normalize-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