2

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()

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)

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.

Mohsin Awan
  • 1,176
  • 2
  • 12
  • 29
Trarbish
  • 363
  • 4
  • 16

1 Answers1

1

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()

UPDATE

I found out that there are 2 buttons with same CSS selector- the first one is invisible, so you get ElementNotVisibleException

You can use

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]
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Thank you. I have just tried it, but it still does not work( – Trarbish Jan 24 '17 at 19:28
  • same error log? Does the button actually hiden when you open required `URL` in browser? If it hidden, was page scrolled with my code? – Andersson Jan 24 '17 at 19:30
  • Yes, the same log. Actually it should not be hidden according to html code, I reckon. How can I understand is it visible for selenium or not? – Trarbish Jan 24 '17 at 19:36
  • Same as for you :) If you see it in opened browser window, then `selenium` "see" it also – Andersson Jan 24 '17 at 19:50
  • Well, I definitely can see it ) – Trarbish Jan 24 '17 at 20:02
  • Note that if CV was already updated button becomes disabled and cannot be clicked, so your script won't work again till next tuesday. Check whether there is `disabled=''` attribute present in button `HTML` code – Andersson Jan 24 '17 at 20:08
  • That's true, the element has “disabled” attribute and it isn't clickable. However, it doesn't matter. I can run the code on my PC and no exceptions appear (cv wouldn't be updated of course). The button element is still visible for Selenium on my PC and isn't on the laptop. – Trarbish Jan 24 '17 at 20:19
  • Thank you! I have actually checked this thing (double element) before but haven't checked this completely. :( You rock! The webpage uses different visible elements depending on size of the window indeed. – Trarbish Jan 25 '17 at 16:52
  • One last question, can I see that an element is invisible using "Inspect tool"/F12 in Chrome? I checked CSS but cannot see any properties like "hidden" or "visibility". I understand that if the element is visible it would be highlighted on the web page, but I am interested how it looks like in the code. – Trarbish Jan 25 '17 at 18:08
  • I think this is because of `classes` applied on higher levels (ancestors). You can check if element is visible (at least in `Firefox` `HTML` code inspector): visible elements appears as dark-blue colored while invisible as light-blue colored. To be sure, find `
    ` and remove `classes` that contains numbers. When it looks like `
    ` you can see same `div` with `Refresh` button from left and right side of page
    – Andersson Jan 25 '17 at 19:23
  • Oh, I see. Thanks a lot! – Trarbish Jan 25 '17 at 19:50