我在以下站点:https://www.ces-us.com/index.asp
我正在使用以下代码:
path_to_chromedriver = r'C:chromedriver' # change path as needed
browser = webdriver.Chrome(executable_path=path_to_chromedriver)
url = 'https://www.ces-us.com/index.asp'
browser.get(url)
username = browser.find_element_by_id("username") #error on this statement
password = browser.find_element_by_id("password")
username.send_keys("my username")
password.send_keys("my password")
browser.find_element_by_name("submit").click()
但对于所有不同的find_
函数,我都会得到以下错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"}
(Session info: chrome=80.0.3987.163)
即使HTML
看起来如下:
div class="col-md-3">
<div class="widget-item">
<div class="request-information">
<h4 class="widget-title">Customer Login</h4>
<form class="request-info clearfix" method="post" action="_login.asp">
<div class="full-row">
<label for="username">Username:</label>
<input type="text" id="uid" maxlength="15" name="uid">
</div> <!-- /.full-row -->
<div class="full-row">
<label for="password">Password:</label>
<input type="password" id="pwd" maxlength="15" name="pwd">
</div> <!-- /.full-row -->
您的ID不正确,请尝试以下解决方案:
wait = WebDriverWait(browser , 10)
browser.get("https://www.ces-us.com/index.asp")
wait.until(EC.presence_of_element_located((By.ID, "uid"))).send_keys("my username")
wait.until(EC.presence_of_element_located((By.ID, "pwd"))).send_keys("my pwd")
wait.until(EC.presence_of_element_located((By.XPATH, "//input[@class='mainBtn pull-left']"))).click()
注意:请将以下导入添加到您的解决方案中
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait