下面是硒python代码,我试图通过selenium发送登录详细信息来单击“登录”。但是,当我使用find_element_by_id
方法查找用户名和密码输入区域时,脚本会引发错误
Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="usernameOrEmail"]"}
。但是,当我检查输入文字类型的网页时,它会显示出我在脚本中提到的相同ID。
P.S:当硒打开浏览器时,请最大化窗口,否则代码将不起作用
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome(executable_path='C://Arighna/chromedriver.exe')
driver.get("https://www.fool.com/")
print(driver.title)
mybutton = driver.find_element_by_id('login-menu-item')
mybutton.click()
delay = 5
WebDriverWait(driver,delay)
email_area = driver.find_element_by_id('usernameOrEmail')
email.send_keys(Keys.ENTER)
email_area.send_keys('ar')
WebDriverWait(driver,delay)
pwd_area = driver.find_element_by_id('password')
pwd_area.send_keys(Keys.ENTER)
pwd_area.send_keys('1234')
WebDriverWait(driver,delay)
login_btn = driver.find_element_by_id('btn-login')
login_btn.click()
我们非常感谢您的帮助。
答案 0 :(得分:0)
我修改了上面的代码,使之生效。
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path='C://Arighna//chromedriver.exe')
driver.get("https://www.fool.com/")
mybutton = driver.find_element_by_id('login-menu-item')
mybutton.click()
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[@id='usernameOrEmail']"))
)
driver.find_element_by_xpath("//input[@id='usernameOrEmail']").send_keys("Selenium")
WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//input[@id='password']"))
)
driver.find_element_by_xpath("//input[@id='password']").send_keys("Selenium")
singin_bttn = driver.find_element_by_id('btn-login')
singin_bttn.click()
答案 1 :(得分:0)
要将字符序列发送到电子邮件/用户名字段,您需要为element_to_be_clickable()
引出WebDriverWait,并且可以使用以下任一方法以下Locator Strategies:
使用CSS_SELECTOR
:
driver.get('https://www.fool.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#login-menu-item"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#usernameOrEmail"))).send_keys("SouradipRoy")
使用XPATH
:
driver.get('https://www.fool.com/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='login-menu-item']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='usernameOrEmail']"))).send_keys("SouradipRoy")
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
您可以在NoSuchElementException上找到一些相关的讨论: