[英]Finding element to click in html with Selenium and Python
我一直在努力定位 HTML 中的元素 - 使用 selenium/python。
我有以下標識一個按鈕;
<button class="btn__primary--large from__button--floating" data-litms-control-urn="login-submit" type="submit" aria-label="Sign in">Sign in</button>
我試圖點擊使用;
driver.find_element_by_class_name('btn__primary--large from__button--floating').click()
但是,我收到錯誤消息:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn__primary--large from__button--floating"}
任何尋找元素的一般幫助將不勝感激!
您的 class 名稱中有一個空格 ( " "
)。 你必須用一個點( .
)來分隔它。
代替
driver.find_element_by_class_name('btn__primary--large from__button--floating').click()
嘗試:
driver.find_element_by_class_name('btn__primary--large.from__button--floating').click()
您也可以找到帶有“登錄”文本的按鈕。
driver.find_element_by_xpath("//button[text()='Sign in']").click()
要單擊登錄元素,您可以使用以下任一定位器策略:
使用css_selector
:
driver.find_element_by_css_selector("button[data-litms-control-urn='login-submit'][aria-label='Sign in']").click()
使用xpath
:
driver.find_element_by_xpath("//button[@aria-label='Sign in' and text()='Sign in']").click()
理想情況下,要單擊需要為element_to_be_clickable()
誘導WebDriverWait的元素,您可以使用以下任一Locator Strategies :
使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-litms-control-urn='login-submit'][aria-label='Sign in']"))).click()
使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Sign in' and text()='Sign in']"))).click()
注意:您必須添加以下導入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.