如何解决硒无法单击按钮
Pic of the website's inspect element More in Depth pic 我的代码段
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
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
from time import sleep
import requests
///
excel = driver.find_element_by_name('Excel')
excel.click()
然后当我尝试运行它时得到它,任何帮助将不胜感激
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="Excel"]"}
解决方法
所需元素是Angular元素,因此,单击该元素必须为element_to_be_clickable()
引出WebDriverWait,并且可以使用以下任一Locator Strategies :
-
使用
CSS_SELECTOR
:WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[mat-button] > span.mat-button-wrapper span"))).click()
-
使用
XPATH
:WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//button[@mat-button]/span[@class='mat-button-wrapper']//span[text()='Excel']"))).click()
-
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
driver.find_element_by_name('Excel')选择一个名称为'Excel'的标签。
例如,它将找到这样的标签:
<div name='Excel'>Hello</div>.
find_element_by_name的CSS等效项是[name =“ Excel”]。
从您的网站的inspect元素图片中,您似乎正在尝试在div中查找文本为“ Excel”的元素,因此,您需要使用以下功能:
driver.find_element_by_link_text('Excel')
希望有帮助!
有关Python Selenium Webdriver的更多信息,请使用this link。这对我很有帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。