硒无法通过XPath找到元素(尽管在浏览器中可见)

时间:2020-03-17 20:56:30

标签: python html selenium xpath webdriver

因此,我试图获取该网站的左侧栏:https://experience.arcgis.com/experience/685d0ace521648f8a5beeeee1b9125cd,以获取当前的WHO冠状病毒数据。我正在按Xpath定位国家/地区,我的代码是:

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)

但是当我运行它时,列表是空的,这很奇怪,因为当Firefox使用Selenium显示页面时,页面看起来完全一样。那么浏览器为什么找不到元素?

3 个答案:

答案 0 :(得分:1)

或者,您可以直接在此处获取数据:

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

解析json,然后执行您想要的任何操作。 covid_data_who

答案 1 :(得分:0)

首先输入iframe driver.switch_to.frame(driver.find_element_by_css_selector("/html/body/div[2]/div[1]/div/div/div/div/div/div/div/iframe"))

country = driver.find_element_by_xpath("//*[@id="Cases_by_country_pt_V3_6708_0_layer"]/circle[32]")
country .click()

请勿复制“完整xpath”,应复制“ xpath”

答案 2 :(得分:0)

由于所需元素位于<iframe>中,因此要访问所需元素,您必须:

  • 为所需的frame_to_be_available_and_switch_to_it()引入 WebDriverWait
  • 为所需的visibility_of_all_elements_located()引入 WebDriverWait
  • 您可以使用以下Locator Strategies

    driver.get("https://experience.arcgis.com/experience/685d0ace521648f8a5beeeee1b9125cd")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.TAG_NAME,"iframe")))
    print([my_elem.text for my_elem in WebDriverWait(driver,20).until(EC.visibility_of_all_elements_located((By.XPATH, "//strong[contains(., 'areas or territories with cases')]//following::div[1]//div[@class='external-html']//span")))])
    
  • 控制台输出:

    ['China :81151 cases', 'Italy :31506 cases', 'Iran (Islamic Republic of) :16169 cases', 'Spain :11178 cases', 'Republic of Korea :8413 cases', 'France :7652 cases', 'Germany :7156 cases', 'United States of America :4356 cases', 'Switzerland :2650 cases', 'The United Kingdom :1954 cases', 'Netherlands :1705 cases', 'Belgium :1486 cases', 'Austria :1471 cases', 'Norway :1423 cases', 'Sweden :1167 cases', 'Denmark :1044 cases', 'Canada :993 cases', 'Japan :873 cases', 'International conveyance (Diamond Princess) :712 cases', 'Malaysia :673 cases', 'Australia :510 cases', 'Czechia :464 cases', 'Portugal :448 cases', 'Qatar :442 cases', 'Greece :387 cases', 'Finland :319 cases', 'Israel :304 cases', 'Ireland :292 cases', 'Slovenia :275 cases', 'Singapore :266 cases', 'Estonia :258 cases', 'Poland :246 cases', 'Romania :246 cases', 'Pakistan :241 cases', 'Bahrain :237 cases', 'Brazil :234 cases', 'Iceland :225 cases', 'Thailand :212 cases', 'Luxembourg :203 cases', 'Chile :201 cases', 'Egypt :196 cases', 'Philippines :187 cases', 'Indonesia :172 cases', 'Saudi Arabia :171 cases', 'Iraq :154 cases', 'India :147 cases', 'Kuwait :142 cases', 'Lebanon :120 cases', 'Peru :117 cases', 'Russian Federation :114 cases', 'United Arab Emirates :113 cases', 'San Marino :104 cases', 'Turkey :98 cases', 'Slovakia :97 cases', 'Mexico :93 cases', 'Panama :86 cases', 'Serbia :85 cases', 'South Africa :85 cases', 'Bulgaria :81 cases', 'Argentina :79 cases', 'Latvia :71 cases', 'Viet Nam :66 cases', 'Colombia :65 cases', 'Croatia :65 cases', 'Algeria :60 cases', 'Ecuador :58 cases', 'Hungary :58 cases', 'Brunei Darussalam :56 cases', 'Albania :55 cases', 'Armenia :52 cases', 'Costa Rica :50 cases', 'Morocco :49 cases', 'Jordan :48 cases', 'Faroe Islands :47 cases', 'Belarus :46 cases', 'occupied Palestinian territory :44 cases', 'Sri Lanka :42 cases', 'Georgia :38 cases', 'Malta :38 cases', 'Venezuela (Bolivarian Republic of) :36 cases', 'Azerbaijan :34 cases', 'Cambodia :33 cases', 'Cyprus :33 cases', 'Kazakhstan :33 cases', 'Oman :33 cases', 'North Macedonia :31 cases', 'Senegal :31 cases', 'Republic of Moldova :30 cases', 'Uruguay :29 cases', 'Guadeloupe :27 cases', 'Bosnia and Herzegovina :26 cases', 'Lithuania :26 cases', 'Tunisia :24 cases', 'Afghanistan :22 cases', 'Dominican Republic :21 cases', 'Burkina Faso :20 cases', 'New Zealand :20 cases', 'Andorra :16 cases', 'Martinique :16 cases', 'Uzbekistan :16 cases', 'Ukraine :14 cases', 'Maldives :13 cases', 'Bolivia (Plurinational State of) :12 cases', 'Jamaica :12 cases', 'Réunion :12 cases', 'French Guiana :11 cases', 'Paraguay :11 cases', 'Bangladesh :10 cases', 'Cameroon :10 cases', 'Monaco :9 cases', 'Honduras :8 cases', 'Rwanda :8 cases', 'Democratic Republic of the Congo :7 cases', 'Ghana :7 cases', 'Liechtenstein :7 cases', 'Ethiopia :6 cases', 'Guatemala :6 cases', 'Côte d’Ivoire :6 cases', 'Cuba :5 cases', 'Jersey :5 cases', 'Mongolia :5 cases', 'Trinidad and Tobago :5 cases', 'Guam :5 cases', 'Guyana :4 cases', 'Kenya :4 cases', 'Seychelles :4 cases', 'Aruba :4 cases', 'Curacao :3 cases', 'Equatorial Guinea :3 cases', 'French Polynesia :3 cases', 'Gabon :3 cases', 'Gibraltar :3 cases', 'Mayotte :3 cases', 'Nigeria :3 cases', 'Puerto Rico :3 cases', 'Saint Barthelemy :3 cases', 'Namibia :2 cases', 'Saint Martin :2 cases', 'Saint Lucia :2 cases', 'United States Virgin Islands :2 cases', 'Liberia :2 cases', 'Montenegro :2 cases', 'Barbados :2 cases', 'Antigua and Barbuda :1 cases', 'Bhutan :1 cases', 'Cayman Islands :1 cases', 'Central African Republic :1 cases', 'Congo :1 cases', 'Eswatini :1 cases', 'Guernsey :1 cases', 'Guinea :1 cases', 'Holy See :1 cases', 'Mauritania :1 cases', 'Nepal :1 cases', 'Saint Vincent and the Grenadines :1 cases', 'Sudan :1 cases', 'Togo :1 cases', 'Suriname :1 cases', 'Somalia :1 cases', 'Benin :1 cases', 'United Republic of Tanzania :1 cases', 'Bahamas :1 cases', 'Gambia :1 cases', 'Djibouti :1 cases', 'Montserrat :1 cases']
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC