[英]Changing focus to a new window in Python/Selenium
就像標題所說的那樣,我無法使用“driver.switch_to_window”讓我的代碼專注於新窗口。
它似乎並沒有關注新窗口,不知道為什么,它可能是我需要在我缺少的代碼開頭“導入”的任何東西,因為 Selenium 文檔沒有真正解釋“導入”或“來自”部分
我的任務是:單擊父頁面上的按鈕 > 出現新窗口 > 單擊新窗口上的“接受”按鈕並繼續執行其余代碼
driver.get("https:testpage.com/")
driver.find_element_by_xpath("/html/body/div[1]/div[1]/main/div/div/div/div[2]/ul/li[1]/button").click()
#here the window appears
time.sleep(2)
driver.switch_to_window("windowName")
driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[3]/button[2]").click() #here nothing happens
您可以嘗試獲取頁面源,以便確定驅動程序是否已切換:
html_source = browser.page_source
它可能已切換,但您的元素未加載或在 iframe 中。 如果您的元素存在,那么您可以嘗試使用不同的相對 XPath,例如找到最近的 id 並從中找到您的元素:
//div[@id='someid']//button[text()='someText']
我不使用絕對 XPath,因為我認為它們太脆弱了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.