2 回答
TA贡献1803条经验 获得超6个赞
induce WebDriverWait() 和 wait for element_to_be_clickable() 以及后面的 css 选择器。
driver.get("https://worldpopulationreview.com/countries/countries-by-gdp")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a[download='csvData.csv']"))).click()
您需要导入以下库。
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
TA贡献1951条经验 获得超3个赞
有时,如果有东西挡住了,selenium 将无法单击某个元素。在这种情况下,您可以使用 JavaScript。但首先我会等待该元素可点击。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import os
driver = webdriver.Chrome()
url = 'https://worldpopulationreview.com/countries/countries-by-gdp'
driver.get(url)
xpath = '/html/body/div[1]/div/div[1]/div[2]/div[2]/div[1]/div/div/div/div[2]/div[1]/a[2]'
# btn = driver.find_element_by_xpath(xpath)
btn = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[@download='csvData.csv']")))
driver.execute_script("arguments[0].click();", btn)
# btn.click()
# df = pd.read_csv(os.path.expanduser('~/Downloads/data.csv'))
# print(df.head())
# driver.close()
添加回答
举报