問題在于接受 cookie,我不確定為什么沒有任何效果,嘗試過By.CLASS, By.NAME等。"Sutinku su visais"必須選擇接受 cookie
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
PATH = Service("C:\\Program Files (x86)\\chromedriver.exe")
driver = webdriver.Chrome(service=PATH)
driver.get("https://www.telia.lt/privatiems")
driver.implicitly_wait(5)
link = driver.find_element(By.CLASS_NAME, "btn btn-primary js-cookie-modal-accept")
link.click()
無論我暫停/等待多長時間,都是同樣的問題:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to
locate element
如果有人能教我如何根據不同的標準在 HTML 中找到我想要的按鈕,我將不勝感激,因為我嘗試過的已經無法實作任何目標。
我試圖接受 cookie 的 HTML:
<div class="button-group button-group--pull-left" data-gtm-vis-has-fired-2746359_1882="1">
<input class="btn btn-primary js-cookie-modal-accept" type="submit" value="Sutinku su visais" data-gtm-vis-has-fired-2746359_1882="1">
<a class="btn btn-link js-cookie-modal-settings" data-gtm-vis-has-fired-2746359_1882="1">
Slapuk? nustatymai
</a>
</div>
uj5u.com熱心網友回復:
Selenium 中的類名不支持空格或多個空格 CLASS_NAME
請隱藏這個
btn btn-primary js-cookie-modal-accept
CSS selcetor通過放置.和洗掉空格進入 a 。
.btn.btn-primary.js-cookie-modal-accept
所以你的有效代碼塊是:
link = driver.find_element(By.CSS_SELECTOTR, ".btn.btn-primary.js-cookie-modal-accept")
link.click()
根據 OP 的要求,這是檢查 HTML DOM 的一種方法
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
我還建議您使用 WebDriverWait 驅動的 ExplicitWait
try:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".btn.btn-primary.js-cookie-modal-accept"))).click()
except:
print("Could not click")
pass
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/369598.html
上一篇:解釋硒如何搜索元素
