我正在try 单击一个按钮,以自动从this site中导出CSV文件.我try 通过按钮名称和XPath查找元素,但引发了"NoSuchElement"异常.我try 了WebdriverWait和Time的变体.睡眠以确保按钮加载,并使用XPath查找器扩展来获得正确的XPath.
代码如下:
#import Packages
import selenium
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
#set URL, Get it, and wait
driver = webdriver.Chrome()
driver.get('https://www.egle.state.mi.us/RIDE/inventory-of-facilities/facilities')
WebDriverWait(driver,11)
#Set and Click button
button = driver.find_element(by=By.XPATH, value='//*[@id="maincontent"]/div/inventory-of-facilities/som-page-section/div/div[2]/div[2]/facilitielayui-table/som-page-section/div/div[2]/div[2]/som-table/div/div/div[1]/div[2]/div[2]/button')
button.click()
The button I'm trying to access is shown in image below: Before posting I've referenced the following Questions:
- Clicking a button with Selenium button个
- How to press/click the button using Selenium if the button does not have the Id?个
- AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
*刚接触Selify,不太精通HTML.任何洞察力都是非常值得赞赏的.