第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Selenium - 無(wú)法在動(dòng)態(tài)元素中獲取 xpath

Selenium - 無(wú)法在動(dòng)態(tài)元素中獲取 xpath

富國(guó)滬深 2022-12-06 15:07:17
我試圖獲取 Google Trends 中一個(gè)元素的 xpath,該元素似乎是動(dòng)態(tài)的,導(dǎo)致控制臺(tái)中出現(xiàn)奇怪的重新加載,這不允許我獲取路徑。因此,我也嘗試通過(guò)我看到的 id 進(jìn)行選擇,但仍然無(wú)法正常工作。我想做的是在搜索框中添加一個(gè)標(biāo)題為“添加搜索詞”的比較查詢(在第一次單擊同一元素后)。這是一個(gè)示例網(wǎng)址:https ://trends.google.com/trends/explore?q=python%20programming&geo=US也許我需要等待?當(dāng)我嘗試在控制臺(tái)中檢查時(shí),我對(duì)隱藏的 html 感到困惑。# click to add and compare querydriver.find_element_by_xpath('//*[@id="explorepage-content-header"]/explore-pills/div/button/span/span[1]').click()time.sleep(10)# find comparisson search boxdriver.maximize_window() driver.implicitly_wait(20) ele = driver.find_element_by_id('input-139')time.sleep(1)ele.send_keys('r programming') <-- im not able to add this query in the comparison boxele.send_keys(Keys.RETURN)這是錯(cuò)誤信息。NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="input-139"]"}  (Session info: chrome=81.0.4044.138)
查看完整描述

2 回答

?
慕桂英3389331

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

該頁(yè)面似乎在頁(yè)面重新加載期間和在不同的瀏覽器上生成不同的 ID。我假設(shè)這很可能與頁(yè)面使用角度有關(guān)。


我使用了以下代碼并能夠使其正常工作,但我假設(shè)我們總是要進(jìn)入第二個(gè)搜索框。第一個(gè)搜索框是原始術(shù)語(yǔ)。


search_boxes = driver.find_elements_by_css_selector('input[aria-label="Add a search term"]')

target_box = search_boxes[1] # Second Box, we're assuming there is always one term.


target_box.send_keys('r programming')

target_box.send_keys(Keys.RETURN)


查看完整回答
反對(duì) 回復(fù) 2022-12-06
?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個(gè)贊

具有動(dòng)態(tài) ID 的輸入字段,您不能使用.find_element_by_id('input-139'). 并嘗試添加WebDriverWait如下:


driver.get('https://trends.google.com/trends/explore?q=python%20programming&geo=US')

compare = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'add-term-text')))

compare.click()

input_elmnt = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '#explorepage-content-header > explore-pills > div > div:nth-child(2)')))

action = ActionChains(driver)

action.move_to_element(input_elmnt).send_keys('r programming').send_keys(Keys.ENTER).perform()

導(dǎo)入后:


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 import ActionChains

from selenium.webdriver.common.keys import Keys


查看完整回答
反對(duì) 回復(fù) 2022-12-06
  • 2 回答
  • 0 關(guān)注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)