找不到元素,也无法定义名称“ ...”

时间:2019-07-08 14:02:24

标签: python selenium-webdriver webdriverwait

我想构建一个自动登录机器人或其他东西。它分为两个部分:  1.登录  2.验证登录 登录部分的最后一行找到并单击一个按钮(登录)。 当我只运行登录部分时,它可以工作,但是当我添加验证行时 它不起作用,找不到要单击的按钮。

然后我使用“显式等待”,但是它不能定义名称“ element”。我很困惑。

代码如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("My Website")
username = driver.find_element_by_xpath("//input[@name='username']")
password = driver.find_element_by_xpath("//input[@name='password']")
username.clear()
username.send_keys("My Username")
password.clear()
password.send_keys("My Password")
driver.find_element_by_xpath("//div[@type='submit']").click()
if len(driver.find_elements_by_xpath("//input[@name='username']"))>0:
    print("Not!")
else:
    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("My Website")
username = driver.find_element_by_xpath("//input[@name='username']")
password = driver.find_element_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.presence_of_element_located((By.XPATH, "//div[@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!")

我的网站是 https://www.instagram.com/accounts/login/ 如果有帮助的话。

第一个代码的结果:

>>> from selenium import webdriver
>>> from selenium.webdriver.common.keys import Keys
>>> driver = webdriver.Firefox()
>>> driver.get("My Website")
>>> username = driver.find_element_by_xpath("//input[@name='username']")
>>> password = driver.find_element_by_xpath("//input[@name='password']")
>>> username.clear()
>>> username.send_keys("My Username")
>>> password.clear()
>>> password.send_keys("My Password")
>>> driver.find_element_by_xpath("//div[@type='submit']").click()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\A\Python3.7.3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "E:\A\Python3.7.3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "E:\A\Python3.7.3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "E:\A\Python3.7.3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //div[@type='submit']

>>> if len(driver.find_elements_by_xpath("//input[@name='username']"))>0:
...     # Element is present
...     print("Not!")
... else:
...     # Element is not present
...     print ("Logged In!")
...
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("My Website")
>>> username = driver.find_element_by_xpath("//input[@name='username']")
>>> password = driver.find_element_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.presence_of_element_located((By.XPATH, "//div[@type='submit']"))
...     )
... finally:
...     element.click()
...
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "E:\A\Python3.7.3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 6, in <module>
NameError: name 'element' is not defined
>>> 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!

:显示已登录!因为我通过错误消息验证登录名,该错误消息说用户名或密码无效。如果未单击登录按钮,则消息不会出现,并且机器人(程序,代码,...)认为您已登录。

2 个答案:

答案 0 :(得分:0)

登录页面中没有type='submit'的div。

按如下所示更改“登录”按钮的xpath。

//button[.='Log In']

答案 1 :(得分:0)

使用WebdriverWaitelement_to_be_clickable。但是,您在xpath中提供了错误的标签名称。标记名称应改为div

而不是button

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