Selenium 没有通过 XPath 找到元素(虽然在浏览器中可见) [英] Selenium not finding element by XPath (although visible in browser)

查看:29
本文介绍了Selenium 没有通过 XPath 找到元素(虽然在浏览器中可见)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试获取该网站的左侧栏:

So I'm trying to get the left bar of this website: https://experience.arcgis.com/experience/685d0ace521648f8a5beeeee1b9125cd to get the current WHO coronavirus data. I'm locating the countries by Xpath, my code is:

from selenium import webdriver

import time


driver = webdriver.Firefox()

driver.get("link_to_the_page")

time.sleep(30)


countries = country = "/html/body/div/div/div[2]/div/div/div/margin-container/full-container/div[10]/margin-container/full-container/div/div[2]/nav/span/div/div/p/span"

countries_name = driver.find_elements_by_xpath(countries)

But when I run this, the list is empty which is weird because the page looks exactly the same when being displayed by Firefox using Selenium. So why can't the browser find the elements?

解决方案

Alternatively you can get the data directly here :

https://services.arcgis.com/5T5nSi527N4F7luB/arcgis/rest/services/Historic_adm0_v3/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=ADM0_NAME%2Ccum_conf%2CDateOfDataEntry&orderByFields=DateOfDataEntry%20asc&resultOffset=2000&resultRecordCount=2000&cacheHint=true

Parse the json, and do whatever you want.

这篇关于Selenium 没有通过 XPath 找到元素(虽然在浏览器中可见)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆