我試圖使用Selenium和xpath在一個網頁中點擊一個按鈕。我成功地點擊了前一個頁面中的一個按鈕,但在加載到這個新頁面后,我試圖使用相同的代碼,但不同的按鈕HTML,它無法像以前那樣加載。
我的代碼如下:
我的代碼如下:
driver = webdriver.Chrome()
driver.get("https://connect.com")
driver.find_element_by_xpath("//button[@class='p-button p-component']"/span>).click() #button workable
在下一個頁面。
driver.find_element_by_xpath("//button[@class='add iconly proto-button ng-star-inserted']"/span>).click() #button not working
我收到的錯誤資訊是:
。selenium.common.exceptions.ElementNotInteractableException: 訊息:元素不可互動。
我還試著加入了如下的等待時間,但還是沒有效果。
button = WebDriverWait(driver, 10).until(ec. element_to_be_clickable((By.XPATH, "//button[@class='add ic-only proto-button ng-star-inserted']"/span>))
button.click()
然而,現在我收到的錯誤資訊是:
selenium.common.exceptions.TimeoutException: 訊息。
另外,我也嘗試在HTML代碼的其他元素上下功夫,比如說。但無論如何,這個按鈕都沒有作業。
driver. find_element_by_xpath("//span[text()='proto-icon ng-star-inserted']").click()
或
驅動。 find_element_by_xpath("//connect-button[@class='proto-button-list-item ng-star-inserted']")。 點擊()
我不知道我還能如何解決這個問題。
uj5u.com熱心網友回復:
我同意@Lucasnguyen17的觀點
按鈕元素肯定是不能點擊的,因為它上面有connect-button元素。
所以
element = driver.find_element_by_xpath("//button[@class='p-button p-component']"/span>)
driver.execute_script("arguments[0].click();", element)
這真的很簡單,
在第一行,它將元素存盤為一個python變數。 然后它運行js腳本。element.click()。
這里的元素是你的特定按鈕。你可以在控制臺中嘗試一下,測驗它是否作業。
如果有一個指向具體頁面的鏈接,將會很有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/320715.html
標籤:
