我想构建一个自动登录机器人(程序或某物)。我想知道它是否使用给定的密码登录。因此,我想验证错误消息,指出用户名或密码无效,并且该消息是否打开了程序,或者自动登录机器人显示了“未登录”警报,并且如果该消息没有打开机器人鞋,警报“已登录”,但是当我给漫游器输入错误的密码并且出现“无效密码”消息时,漫游器也会显示警报“已登录”! 另一个问题:当我要构建* .py文件时,代码“ print(已登录)”有效,但是“已登录”警报关闭得非常快,无法读取警报。
这是我的第一个也是主要问题的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://www.instagram.com/accounts/login/")
username =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']")))
password =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
username.clear()
username.send_keys("My Username")
password.clear()
password.send_keys("My Password")
try:
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))
)
finally:
element.click()
if len(driver.find_elements_by_xpath("//form[@id='slfErrorAlert']"))>0:
# Element is present
print("Not!")
else:
# Element is not present
print ("Logged IN!")
这是我每次得到的:
>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>>
>>> driver = webdriver.Firefox()
>>> driver.get("https://www.instagram.com/accounts/login/")
>>>
>>> username =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']")))
>>> password =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
>>> username.clear()
>>> username.send_keys("My Username")
>>> password.clear()
>>> password.send_keys("My Password")
>>> try:
... element = WebDriverWait(driver, 10).until(
... EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))
... )
... finally:
... element.click()
...
>>> if len(driver.find_elements_by_xpath("//form[@id='slfErrorAlert']"))>0:
... # Element is present
... print("Not!")
... else:
... # Element is not present
... print ("Logged IN!")
...
Logged IN!
答案 0 :(得分:1)
尝试以下代码。如果您输入了错误的用户名或密码,则应返回Not!并且如果您提供有效的用户名和密码,它应该在隐式等待的帮助下返回登录状态。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("https://www.instagram.com/accounts/login/")
username =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']")))
password =WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
username.clear()
username.send_keys("My Username")
password.clear()
password.send_keys("My Password")
try:
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']"))
)
finally:
element.click()
driver.implicitly_wait(2)
if len(driver.find_elements_by_xpath("//p[@id='slfErrorAlert']"))>0:
# Element is present
print("Not!")
else:
# Element is not present
print ("Logged IN!")