我很担心以下问题。这是一个更新网站简历的简单脚本。
#!/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()
此代码在我的电脑上完美运行,但当我在笔记本电脑上运行时,出现“元素不可见”错误:
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个)。任何想法为什么这个代码不能在我的笔记本电脑上工作 我尝试使用显式和隐式等待,但元素仍然是不可见的。
答案 0 :(得分:1)
这可能是因为您的显示器和笔记本电脑的屏幕分辨率不同:目标按钮在显示器上可见,分辨率较高,在低分辨率屏幕的笔记本电脑上不可见。
为了能够点击目标按钮,您可能需要向下滚动页面,如下所示:
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]