簡體   English   中英

Python Selenium - 獲取屬於網頁特定部分的元素的 TimeoutException

[英]Python Selenium - Getting TimeoutException for elements belonging to a particular section of a webpage

我一直在嘗試單擊屬於網站中切換開關的這個特定按鈕。

<button _ngcontent-plj-c265="" type="button" class="glyphicon glyph-mini ng-star-inserted expand" aria-expanded="true" aria-label="Title Expand or collapse filter card" style="visibility: visible;" pbi-focus-tracker-idx="6"></button>

這存在於網站的“過濾器”部分,我采用了幾種方法和標簽來執行此操作,其中一些我已放在這里:

1. sample1 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("xpath", '//*[@id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]'))).click()

2. sample2 = ui.WebDriverWait(driver, 60).until(EC.element_to_be_clickable(("css selector", 'button.expand'))).click()

3.sample3 = driver.find_element("xpath",'//[@id="exploreFilterContainer"]/div[2]/div/filter[14]/div/div[1]/div[1]/button[1]').click()

方法 3 給了我一個 NoSuchElementException。 因此,我采用了方法 1 和 2,但都得到了 TimeoutException。 該按鈕沒有 ID,所以我也無法通過這種方式獲取它。

但我注意到按鈕中有一個屬性aria-expanded="true" ,我假設當它取值“false”時將有助於操作切換按鈕。 但要注意的是,如果 Selenium 識別元素本身,它只能這樣做,這讓我們回到原點。

對於我的困境,我非常感謝任何富有成效的答案。 提前致謝。

我能夠通過切換到元素所在的<iframe>來解決我的問題:

driver.switch_to.frame(0)   #switching to the iframe here
tb = driver.find_element("xpath", '//*[@id="exploreFilterContainer"]/div[2]/div/filter[15]/div/div[1]/div[1]/button[1]')
driver.execute_script("arguments[0].click();", tb) #using JS to click the button

並且建議在執行完與元素相關的所有操作后,我們切換回正常狀態:

driver.switch_to.default_content()

感謝@Prophet @simpleApp 為幫助我所做的努力:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM