selenium使用之未解之謎

https://www.outbreak.my/這個網(wǎng)站

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.chrome.options import Options


option = Options()
#option.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=option)
driver.get('https://www.outbreak.my/')
time.sleep(10)

confirmed = driver.find_element_by_id('cases-my-confirmed').text
active = driver.find_element_by_id('cases-my-active').text
death = driver.find_element_by_id('cases-my-death').text

cofirmed_changes = driver.find_element_by_id(
              'cases-my-confirmed-changes').text
death_changes = driver.find_element_by_id('cases-my-death-changes').text

前面的代碼一切正常,問題出現(xiàn)在“用for循環(huán)上面”:

states = driver.find_elements_by_xpath(
              '//div[@class="card-body o-auto"]//tr')

for s in states:
    print(s.find_element_by_css_selector('td.text-value').text)
報錯
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"td.text-value"}
  (Session info: chrome=81.0.4044.138)

但是,如果不用for循環(huán),直接states[0],states[1],states[2],states[3],states[4]... ... 便可以取element,無論是by_css_selector/xpath/class_name

states[3].find_element_by_css_selector('td.text-value').text

Why?

Why?

Why?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容