我的 selenium webdriver 由于以下原因而不斷崩潰
TimeoutException:訊息:超時:從渲染器接收訊息超時:298.972
cookie 彈出打開但腳本沒有點擊它,像 20 driver.get(url), 19 次它會接受 cookie 但第 20 次將無法接受 cookie,雖然視窗已經打開,我嘗試使用下面的代碼但是仍然失敗。
retries = 1
while retries <= 5:
try:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@]'))) #wait until cookies clickable
element.click()
break
except TimeoutException:
driver.refresh()
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@]'))) #wait until cookies clickable
element.click()
retries = 1
uj5u.com熱心網友回復:
嘗試使用 driver.execute_script() 而不是 element.click()
htmlElement = driver.find_element_by_xpath('//*[@]')
driver.execute_script("arguments[0].click();", htmlElement)
uj5u.com熱心網友回復:
一般來說,
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@]'))) #wait until cookies clickable
element.click()
應該管用。從您共享的代碼中,我看不出為什么它在 95% 的情況下有效,但在 5% 的情況下失敗。
我所看到的:
您正試圖在except塊中找到導致TimeoutException.
因此,如果 Selenium 無法找到與此定位器匹配的//*[@]可點擊元素并拋出TimeoutException等待相同條件的元素except會給您相同的結果....另外,我認為將其置于 5 次嘗試的回圈中是沒有意義的。
如果找到元素,單擊并關閉 - 沒有意義再試一次。
如果您在第一次嘗試中無法做到這一點 - 這將在第一次嘗試時拋出TimeoutException例外,您將永遠不會在這里繼續第二次嘗試......
uj5u.com熱心網友回復:
我運行了下面的腳本超過 20 次,但它仍然能夠每次都單擊所需的按鈕。
我所要做的基本上就是將定位器從 XPath 更改為 CSS:
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://www.novasol.com/")
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
print('Clicked it')
except:
print('Either element was not found, or Bot could not click on it.')
pass
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
更新:
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
print('Clicked it')
except:
print('Either element was not found, or Bot could not click on it.')
driver.refresh()
time.sleep(5)
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='CookieInformation.submitAllCategories();']"))).click()
except:
pass
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406019.html
標籤:
下一篇:當Python因為我無法單擊串列而不允許我單擊任何內容時,如何在Python中使用selenium來選擇日歷日期?
