2 回答

TA貢獻2036條經驗 獲得超8個贊
該頁面似乎在頁面重新加載期間和在不同的瀏覽器上生成不同的 ID。我假設這很可能與頁面使用角度有關。
我使用了以下代碼并能夠使其正常工作,但我假設我們總是要進入第二個搜索框。第一個搜索框是原始術語。
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)

TA貢獻1812條經驗 獲得超5個贊
具有動態 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()
導入后:
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
添加回答
舉報