Selenium错误:元素不可见(两台计算机上的不同行为) [英] Selenium Error: element not visible (different behaviour on two computers)

查看:67
本文介绍了Selenium错误:元素不可见(两台计算机上的不同行为)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑以下问题.这是一个简单的脚本,可以更新网站上的简历.

I am pretty stuck with the following problem. This is a simple script which updates CV on the website.

#!/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上可以正常使用,但是当我在笔记本电脑上运行该代码时,会出现错误元素不可见":

This code works perfectly on my PC but when I run it on my laptop the error "element not visible" appears:

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).有什么想法为什么此代码在我的笔记本电脑上不起作用?我尝试使用显式和隐式等待,但是元素仍然不可见.

I use the same version of chrome, chromedriver, python and selenium on my computers. Windows are different (it's 7 on my PC and 8 on my laptop). Any ideas why this code does not work on my laptop? I tried to use explicit and implicit wait, but element is still invisible.

推荐答案

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

This is probably because of screen resolution is different on your monitor and laptop: target button could be visible on monitor with higher resolution and invisible on laptop with low resolution screen.

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

To be able to click on target button you might need to scroll page down as below:

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

I found out that there are 2 buttons with same CSS selector- the first one is invisible, so you get 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]

这篇关于Selenium错误:元素不可见(两台计算机上的不同行为)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆