我有一个Selenium + Python + Chromedriver脚本,该脚本应登录网站并单击下载按钮,该按钮将下载CSV文件。
检查下载按钮的详细信息是:
<button id="csv-button" class="block tiny-margin-top" data-args="csv">
CSV
</button>
XPath是:
//*[@id="csv-button"]
但是它说我运行脚本时找不到XPath元素。请在下面找到代码:
click_button = driver.find_element_by_xpath('//*[@id="csv-button"]')
click_button.click()
如果使用唯一的xpath标识WebElement(如果未发现元素异常),则需要将WebDriverWait与expected_conditions
设置element_to_be_clickable
为以下子句的条件一起使用:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# other code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='csv-button']"))).click()
要更详细一点,您可以使用:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# other code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='block tiny-margin-top' and @id='csv-button']"))).click()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句