元素在使用python和selenium进行自动登录时不可见outlook.com

2024-04-26 15:12:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个脚本,用python和selenium自动登录outlook邮件。但是在邮件地址识别之后,脚本总是停在密码部分。但这两部分完全一样!我一定是哪里错了。真诚地寻求帮助。你知道吗

我的代码如下:

browser.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')

username = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='i0116']")))
username.clear()
username.send_keys(usernameStr)
nextButton = browser.find_element_by_id('idSIButton9')
nextButton.click()

password = WebDriverWait(browser, 50).until(
EC.presence_of_element_located((By.XPATH, "//input[@id='i0118']")))
password.clear()
password.send_keys(passwordStr)
signinButton = browser.find_element_by_id('idSIButton9')
signinButton .click()

错误:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    browser.find_element_by_id('i0118').click()
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium-3.0.2-py2.7.egg\selenium\webdrive
r\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visi
ble
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cf
d9),platform=Windows NT 6.1.7601 SP1 x86)

Tags: inpybrowseridegglibpackagesselenium
1条回答
网友
1楼 · 发布于 2024-04-26 15:12:42

您应该尝试用visibility_of_element_located替换presence_of_element_located条件:

password = WebDriverWait(browser, 50).until(
EC.visibility_of_element_located((By.XPATH, "//input[@id='i0118']")))
password.clear()

相关问题 更多 >