[英]using Selenium in Python to click on the right checkbox
我對使用 selenium 很陌生,但我找不到解決一個非常簡單的任務的方法。 我需要能夠單擊指定卧室的元素: 2. 我用過我不知道 xpath、ID、名稱、class 有多少引用,但 Z8E00596AD8DE2213FF8F8D8478D53 只是找不到元素。 我也嘗試瀏覽互聯網,但找不到對我有幫助的解決方案。
這是截圖
driver.find_element_by_id('agatha_bedrooms1588844814480_advancedSearch1').click()
這行不通。 Selenium 找不到元素。 這個元素似乎在另一個元素中,但我不明白如何訪問它。 請問你能幫幫我嗎?
非常感謝你。
G
如果復選框位於 iframe 內部,請執行以下操作:
# basically just select the iframe any way you want
frame = driver.find_element_by_css_selector("iframe")
driver.switch_to.frame(frame)
driver.find_element_by_id('agatha_bedrooms1588844814480_advancedSearch1').click()
編輯:我找到了解決方案。 有點丑,但有效,哈哈
element = driver.find_elements_by_css_selector("input[name=bedrooms][value='2']")[0]
element.find_element_by_xpath("..").click()
id 似乎是動態生成的,在這種情況下,您不能依賴它們。 試試這個xpath
:
driver.find_element_by_xpath("//*[@name='bedrooms' and @value='2']/following::label").click()
盡管使用等待通常是一種很好的做法。 所以像:
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[@name='bedrooms' and @value='2']/following::label"))).click()
確保有這些進口等待工作
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
非常感謝,經過多次多次試驗,我可以這樣解決:
elemt = driver.find_element_by_xpath("//*@name='bedrooms']").find_element_by_xpath("//[@value='2']")
idvar = elemt.get_attribute("id")
elemt2 = driver.find_element_by_xpath("//label[@for='" + idvar + "']")
elemt2.click()
該復選框似乎隱藏在 label (?.) 下,因此 Selenium 不想點擊它。
你可以試試這個 xpath。 希望它有幫助:
//*[@name='bedrooms']/following::*/*[text()='2']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.