[問題] 用Selenium 定位Instgram搜尋頁無法定位

作者: lalelee (Yes We Can !)   2023-02-16 10:33:11
Instgram(https://www.instagram.com/) 現在登入後在右邊有個搜尋的Icon,
圖:https://imgur.com/a/RIyY1Rm
這個Icon 和 點擊以後出現的搜尋欄位我用XPATH都沒辦法定位成功, 請各位協助
謝謝
#搜尋按鈕XPATH:
XPATH:'//*[@id="mount_0_0_Rh"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/d
iv[1]/div/div/div[1]/div/div[2]/div[2]/div/a/div/div/div/div/svg'
Code:
#搜尋按鈕XPATH定位
search_path =
'//*[@id="mount_0_0_Rh"]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div[1]/div/div[2]/div[2]/div/a/div/div/div/div/svg'
search = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, search_path))
)
search.click()
#按鈕點擊後搜尋欄位XPATH定位
searchbox_xpath = '//*[contains(concat( " ", @class, " " ), concat( " ",
"x1n2onr6", " " ))]'
searchbox = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, searchbox_xpath))
)
keyword = '#Car'
searchbox.send_keys(keyword)
time.sleep(1)
searchbox.send_keys(Keys.RETURN)
作者: lycantrope (阿寬)   2023-02-16 10:49:00
先用Chrome開發人員工具測一下xpath有沒有成功定位吧.
作者: brad0315 (老默)   2023-02-16 22:19:00
用By.PARTIAL_LINK_TEXT找看看,如果有文字的話
作者: adeniss ( )   2023-02-18 15:52:00
看起來搜尋按鈕的path的值會因為每次更新而變化 留意id=建議改其他元素

Links booklink

Contact Us: admin [ a t ] ucptt.com