无法让硒点击按钮

Can't get selenium to click button

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"]"}

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。这对我帮助很大!

所需的元素是 Angular element, so to click on the element you have to induce for the element_to_be_clickable() and you can use either of the following :

  • 使用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