python - Selenium 错误 : element not visible (different behaviour on two computers)

标签 python python-3.x selenium selenium-webdriver automated-tests

我很困于以下问题。这是一个简单的脚本,用于更新网站上的 CV。

#!/usr/bin/env python3

from selenium import webdriver

# Authentication details
LOGIN = input("Please type your email  ---> ")
PASSWORD = input("Please type your password ---> ")

# Chrome is a default browser, change to appropriated one
browser = webdriver.Chrome()
browser.get('https://www.hh.ru/account/login')

# Authentication
emailel = browser.find_element_by_css_selector('input[type="email"]')
emailel.send_keys(LOGIN)
passel = browser.find_element_by_css_selector('input[type="password"]')
passel.send_keys(PASSWORD)
passel.submit()

# Looking for CV's links
browser.get('https://hh.ru/applicant/resumes')
links = []
resume_els = browser.find_elements_by_css_selector('a.b-resumelist-vacancyname')
for r in resume_els:
    links.append(r.get_attribute('href'))

# Update all CVs
for link in links:
    browser.get(link)
    refresh_button = browser.find_element_by_css_selector('button.HH-Resume-Touch-Button')
    refresh_button.click()

# Quit webdriver
browser.quit()

此代码在我的 PC 上运行完美,但是当我在我的笔记本电脑上运行它时,出现错误“元素不可见”:

Traceback (most recent call last):
  File "C:/Python36/test.py", line 36, in <module>
    refresh_button.click()
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
    return self._parent.execute(command, params)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=55.0.2883.87)
  (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 6.3.9600 x86_64)

我在我的计算机上使用相同版本的 chrome、chromedriver、python 和 selenium。 Windows 不同(我的 PC 上是 7,笔记本电脑上是 8)。知道为什么这段代码不能在我的笔记本电脑上运行吗? 我尝试使用显式和隐式等待,但元素仍然不可见。

最佳答案

这可能是因为您的显示器和笔记本电脑的屏幕分辨率不同:目标按钮可能在分辨率较高的显示器上可见,而在笔记本电脑的低分辨率屏幕上不可见。

为了能够点击目标按钮,您可能需要向下滚动页面,如下所示:

refresh_button = browser.find_element_by_css_selector('button.HH-Resume-Touch-Button')
browser.execute_script('arguments[0].scrollIntoView(true);', refresh_button)
refresh_button.click()

更新

我发现有 2 个按钮具有相同的 CSS 选择器 - 第一个是不可见的,所以你得到 ElementNotVisibleException

你可以使用

from selenium.common.exceptions import ElementNotVisibleException
try:
    refresh_button = browser.find_elements_by_css_selector('button.HH-Resume-Touch-Button')[0]
except ElementNotVisibleException:
    refresh_button = browser.find_elements_by_css_selector('button.HH-Resume-Touch-Button')[1]

关于python - Selenium 错误 : element not visible (different behaviour on two computers),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41836942/

相关文章:

python - Elasticsearch 和日期范围过滤器

python - FFmpeg 裁剪 : 'Invalid too big or non positive size for width ' 230 4' or height ' 4096'

python-3.x - 在python中为cplex求解器pyomo设置optimalitytarget参数

python - 检查另一个列表中的字符串列表中是否存在字符串字符?

python - 如何在不等待 30 秒超时的情况下测试元素是否缺失

python - 使用Notepad++时如何删除关联符号?

python - 在多个值处拆分 numpy 数组?

python-3.x - Slack 上未显示交互式响应

应用程序中的 Android Web 自动化

java - 需要一个 SMTP 测试服务器进行集成测试(selenium)