Selenium 无法检测密码字段
Selenium fails to detect password field
如果您访问:http://212.238.166.253:8080/
您会注意到以下 html 元素:
<input type="password" autocapitalization="off" autocomplete="off" value="" name="login_passwd" tabindex="2" class="form_input" maxlength="16" onkeyup="" onpaste="return false;" onblur="" placeholder="Password">
我在python中写道:
import time
import selenium.common.exceptions as SE
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
PASSWORD_XPATH = "//input[(@type='password') or (@type='Password') or (@type='PASSWORD')]"
if __name__ == '__main__':
driver = webdriver.Chrome(service=Service(
'/Users/algo/.wdm/drivers/chromedriver/mac64/100.0.4896.60/chromedriver'))
driver.get('http://212.238.166.253:8080/')
time.sleep(5)
try:
password_input = WebDriverWait(driver, timeout=5).until(
EC.element_to_be_clickable((By.XPATH, PASSWORD_XPATH)))
except SE.TimeoutException:
print('Nothing was Found!')
但我得到的只是:
Nothing was Found!
这是为什么?我仔细检查了一下,页面直接完全上传了,增加超时和休眠时间也没有用。为什么元素存在却找不到?
正如 @Arundeep Chohan
提到的 you have used identifies multiple elements within the HTML DOM.
第一个匹配元素具有 style="display: none; 而 ofcoarse 不是您想要的元素。
<input name="foilautofill" style="display: none;" type="password">
因此,Nothing was Found!
被打印出来。
第二个匹配的节点就是你想要的元素
<input type="password" autocapitalization="off" autocomplete="off" value="" name="login_passwd" tabindex="2" class="form_input" maxlength="16" onkeyup="" onpaste="return false;" onblur="" placeholder="Password">
您需要构建规范 which identifies the desired element uniquely within the DOM Tree。
解决方案
要理想地定位元素,您需要诱导 WebDriverWait for the 您可以使用以下任一定位器策略:
使用CSS_SELECTOR:
password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='password'][name='login_passwd']")))
使用 XPATH:
password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='password' and @name='login_passwd']")))
如果您访问:http://212.238.166.253:8080/
您会注意到以下 html 元素:
<input type="password" autocapitalization="off" autocomplete="off" value="" name="login_passwd" tabindex="2" class="form_input" maxlength="16" onkeyup="" onpaste="return false;" onblur="" placeholder="Password">
我在python中写道:
import time
import selenium.common.exceptions as SE
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
PASSWORD_XPATH = "//input[(@type='password') or (@type='Password') or (@type='PASSWORD')]"
if __name__ == '__main__':
driver = webdriver.Chrome(service=Service(
'/Users/algo/.wdm/drivers/chromedriver/mac64/100.0.4896.60/chromedriver'))
driver.get('http://212.238.166.253:8080/')
time.sleep(5)
try:
password_input = WebDriverWait(driver, timeout=5).until(
EC.element_to_be_clickable((By.XPATH, PASSWORD_XPATH)))
except SE.TimeoutException:
print('Nothing was Found!')
但我得到的只是:
Nothing was Found!
这是为什么?我仔细检查了一下,页面直接完全上传了,增加超时和休眠时间也没有用。为什么元素存在却找不到?
正如 @Arundeep Chohan
提到的
第一个匹配元素具有 style="display: none; 而 ofcoarse 不是您想要的元素。
<input name="foilautofill" style="display: none;" type="password">
因此,Nothing was Found!
被打印出来。
第二个匹配的节点就是你想要的元素
<input type="password" autocapitalization="off" autocomplete="off" value="" name="login_passwd" tabindex="2" class="form_input" maxlength="16" onkeyup="" onpaste="return false;" onblur="" placeholder="Password">
您需要构建规范
解决方案
要理想地定位元素,您需要诱导 WebDriverWait for the
使用CSS_SELECTOR:
password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[type='password'][name='login_passwd']")))
使用 XPATH:
password_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@type='password' and @name='login_passwd']")))