Python/Selenium - "no such element: Unable to locate element"
Python/Selenium - "no such element: Unable to locate element"
我很难在 this website 上定位元素。我的最终目标是收集每个国家的记分卡数据。我设想这样做的方式是使用 selenium 单击列表视图(与默认国家/地区视图相反)并循环进出所有国家/地区概况,沿途抓取每个国家/地区的数据。但是在过去对我有用的页面上定位元素的方法在这个站点上没有结果。
这里有一些示例代码概述了我的问题。
from selenium import webdriver
driver = webdriver.Chrome(executable_path= "C:/work/chromedriver.exe")
driver.get('https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles')
# click the `list` view option
driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[2]/div[2]/svg[2]')
如您所见,我只完成了计划的第 1 步。就增加等待时间而言,我已经尝试了其他问题的建议,但无济于事。我在我的 DOM 中看到该站点已完全加载,但没有 xpath 适用于我能找到的任何元素。如果这个问题发布得太频繁,我深表歉意,但我知道我们非常感谢您提供的所有帮助。谢谢!
您错误地单击了列表视图。您的定位器必须稳定。我检查了它没有用。
所以,为了点击图标,使用 WebDriverWait
:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".sc-gzOgki.ftxBlu>.background")))
list_view = driver.find_element_by_css_selector(".sc-gzOgki.ftxBlu>.background")
list_view.click()
接下来,要获取唯一的行定位器,请使用以下 css
选择器:
.sc-jbKcbu.kynSUT
它将使用所有国家的列表。
该元素位于 iframe 中,您需要切换它才能访问该元素。
使用WebDriverWait()
等待frame_to_be_available_and_switch_to_it()
driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
wait=WebDriverWait(driver,10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[name()='svg' and @class='sc-gxMtzJ bRxjeC']"))).click()
您需要以下导入。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
另一个更新:
driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
wait=WebDriverWait(driver,10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
driver.find_element_by_xpath("//*[name()='svg' and @class='sc-gxMtzJ bRxjeC']").click()
我很难在 this website 上定位元素。我的最终目标是收集每个国家的记分卡数据。我设想这样做的方式是使用 selenium 单击列表视图(与默认国家/地区视图相反)并循环进出所有国家/地区概况,沿途抓取每个国家/地区的数据。但是在过去对我有用的页面上定位元素的方法在这个站点上没有结果。
这里有一些示例代码概述了我的问题。
from selenium import webdriver
driver = webdriver.Chrome(executable_path= "C:/work/chromedriver.exe")
driver.get('https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles')
# click the `list` view option
driver.find_element_by_xpath('//*[@id="root"]/div/div[1]/div[2]/div[2]/svg[2]')
如您所见,我只完成了计划的第 1 步。就增加等待时间而言,我已经尝试了其他问题的建议,但无济于事。我在我的 DOM 中看到该站点已完全加载,但没有 xpath 适用于我能找到的任何元素。如果这个问题发布得太频繁,我深表歉意,但我知道我们非常感谢您提供的所有帮助。谢谢!
您错误地单击了列表视图。您的定位器必须稳定。我检查了它没有用。
所以,为了点击图标,使用 WebDriverWait
:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".sc-gzOgki.ftxBlu>.background")))
list_view = driver.find_element_by_css_selector(".sc-gzOgki.ftxBlu>.background")
list_view.click()
接下来,要获取唯一的行定位器,请使用以下 css
选择器:
.sc-jbKcbu.kynSUT
它将使用所有国家的列表。
该元素位于 iframe 中,您需要切换它才能访问该元素。
使用WebDriverWait()
等待frame_to_be_available_and_switch_to_it()
driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
wait=WebDriverWait(driver,10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
wait.until(EC.element_to_be_clickable((By.XPATH,"//*[name()='svg' and @class='sc-gxMtzJ bRxjeC']"))).click()
您需要以下导入。
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
另一个更新:
driver.get("https://www.weforum.org/reports/global-gender-gap-report-2021/in-full/economy-profiles#economy-profiles")
wait=WebDriverWait(driver,10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "iFrameResizer0")))
driver.find_element_by_xpath("//*[name()='svg' and @class='sc-gxMtzJ bRxjeC']").click()