Can't verify the element

2019-07-29 06:33发布

I want to build an auto-login bot(program or sth). I want to know if it logged in or not with the given password. so I want to verify the error message that says username or password is invalid and if that message turns up the program or the auto-login bot show the alert "not logged in" and if the message doesn't turn up the bot shoe the alert "logged in",but when I give the bot the wrong password and the "invalid password" message turns up the bot show the alert "logged in" too! an other problem: when I want to build a *.py file the code "print ("logged in") works but the "logged in" alert closes very fast that can not read the alert.

Here's code for my first and main problem:

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!")

Here's what I got each time:

>>> 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!

1条回答
Viruses.
2楼-- · 2019-07-29 07:17

Try the following code.If you give wrong username or password it should return Not! and if you provide valid user name and password it should return logged in with the help of implicit wait.

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!")
查看更多
登录 后发表回答