Selenium - Element is Not Visible [Python] - python

I'm currently in CH11 from the "Automate the Boring Stuff with Python" book and I'm going over the Selenium module. I'm trying to move to the end of a page but I'm getting some problems. I also tried to look similar problems in this site and tried the solutions suggested without success unfortunately. Here's my code, when I type it into the IDLE Shell:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser= webdriver.Firefox()
browser.get('http://nostarch.com')
htmlElem= browser.find_element_by_tag_name('html')
type(htmlElem)
<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>
htmlElem.send_keys(Keys.END) # Error
Exception -:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
htmlElem.send_keys(Keys.END)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 347, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 494, in _execute
return self._parent.execute(command, params)
File "C:\Python\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python\Python35\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 is not visible
Apparently the html element is not visible? I don't understand how so since it seems to locates the html element just fine as seen on the code without any problems but the Key.ENTER is where I'm getting the error.
Any help would be appreciated.

Just tested the following with Chrome driver and it works (It should also work with Firefox):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome()
browser.get('http://nostarch.com')
body_elem = browser.find_element_by_tag_name('body')
body_elem.send_keys(Keys.END)

Related

Traceback (most recent call last): File "D:\pythonProject\webscraping.py", line 17, in <module> search.send_keys(Keys.RETURN) Error python

I am following a tutorial of a video on youtube about selenium and I got to this point
This is my code and it works fine until it gives you this massive error of:
Traceback (most recent call last):
File "D:\pythonProject\webscraping.py", line 17, in <module>
search.send_keys(Keys.RETURN)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 602, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 773, in _execute
return self._parent.execute(command, params)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "C:\Users\Han\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: unexpected command response
(Session info: chrome=103.0.5060.53)
....
Here is the code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
service=Service("C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://www.techwithtim.net/")
print(driver.title)
search = driver.find_element(By.NAME, 's')
search.send_keys("test")
search.send_keys(Keys.RETURN)
driver.quit()
I've been getting the same error as well. There seems to be an issue with Chromedriver 103. Your options are to wait for the new version, or to download Chrome Beta and use Chromedriver 104 with it.
A lot of people have also said to downgrade and use 102 instead.
Here's a link to a thread where others have reported the same problem:
https://github.com/SeleniumHQ/selenium/issues/10799
(//input[#name='s'])[2]
There's actually a second input with that value hidden use the above xpath.

have a special web page can't find any elements

The website is https://www.gaitame.com/bo/demo.html
in middle have a button with japanese "デモトレード版を利用する"
then will be go to a demo trader page like this
in this page i cant get any elements
other page is all right
i already switch_to this tab
please help.............
here is my testing code
from selenium import webdriver
import time
url = 'https://www.gaitame.com/bo/demo.html'
driver = webdriver.Chrome()
driver.get(url)
# Click the button and wait for loading
driver.find_element_by_class_name("blue2btn").click()
time.sleep(5)
# Switch to another tab
driver.switch_to.window(driver.window_handles[1])
# Try find any elements
driver.find_elements_by_class_name("header_area")
this is error msg
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-ed6cc8fa6e76>", line 14, in <module>
driver.find_elements_by_class_name("header_area")
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 580, in find_elements_by_class_name
return self.find_elements(by=By.CLASS_NAME, value=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: Unsupported locator strategy: null
(Session info: chrome=80.0.3987.132)
This is a newly introduced Chromedriver bug.
Try using an older version of Chrome or another browser. I just tried your code in Firefox 73 and it worked.
Hope this helps and good luck!
the bug seems to still be in chromedriver.
it's related to switching to a new window or tab.
However, in the OP's post, I don't see how he created the 2nd tab. he just switches to the tab/window with this
driver.switch_to.window(driver.window_handles[1])
It's possible the original problem here, was not creating the 2nd tab/window correctly.
Or it could be the existing chromedriver problem (which is intermittent/dependent on web page)
see here https://bugs.chromium.org/p/chromedriver/issues/detail?id=3390
The exception I'm tracking (which the OP reported) is this
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: Unsupported locator strategy: null

Selenium except NoSuchElementException not working in my python script [duplicate]

This question already has an answer here:
Selenium "selenium.common.exceptions.NoSuchElementException" when using Chrome
(1 answer)
Closed 3 years ago.
I have a very simple code where I am looking for a class and when that class doesn't exist I want to catch that and update a status in my CSV. I am implementing this like:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
invalid = browser.find_element_by_class_name('_3lLzD')
try:
Update status
except NoSuchElementException as e:
Update Status
It still throws me the error:
Traceback (most recent call last):
File "C:/Users/Krenovate/Desktop/automate/automate/automate.py", line 35, in <module>
invalid = browser.find_element_by_class_name('_3lLzD')
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Krenovate\PycharmProjects\mxrecord\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: ._3lLzD
What am I doing wrong
Per the documentation, the locator function is designed to throw an exception if it is not found. Therefore, the exception that you get is well within the expected behavior.
If you are unsure of the presence of the element, the easiest way is to place it within a try block
try:
invalid = browser.find_element_by_class_name('_3lLzD')
Update status
except NoSuchElementException as e:
Update Status
or use explicit waits with a timeout.

CLOSED - Python Selenium Can't find elements

I have a problem with selenium with python 3.5, after install all the windows updates all my selenium script broken, I receive every time the same error:
Traceback (most recent call last):
File "C:/Users/Carlo/Desktop/CEx/src/IE.py", line 12, in
a=driver.find_element_by_xpath("//*[#id='un']")
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: Unable to find element on closed window
But I really don't understand why because the window of IE is open!
This is my code (simple just to check why I can't make it work more):
import time from
selenium import webdriver
driver = webdriver.Ie()
driver.get('http://gala.test-platform.celtrino.com/Login.aspx')
time.sleep(10)
driver.find_element_by_xpath(".//*[#id='un']")
The code fail every time in the last line and I double check with firepath and the xpath is correct so I don't really understand why it's not working.
I knew, IE has problems with xpath. It does not support xpath directly. It needs third party tools to do this. So, I suggest you to try cssSelector or any other options instead. Since, the element has an ID so you could use this. It's better.
driver.find_element_by_id("un");

Issue with selenium-python on Firefox

I am new to Selenium and am following the website http://selenium-python.readthedocs.org/
I tried the following bit of code which is there on the website
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
When I run this program, a Firefox instance opens but never loads the page. check snapshot
This is what I have received in the output console
C:\Users\Gauss\Desktop>python test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
driver = webdriver.Firefox()
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\webdriver.py", line 78, in __init__
self.binary, timeout)
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\extension_connection.py", line 49, in __init__
self.binary.launch_browser(self.profile)
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable()
File "D:\Python\Python35\lib\site-packages\selenium-2.50.0-py3.5.egg\selenium\webdriver\firefox\firefox_binary.py", line 106, in _wait_until_connectable
% (self.profile.path))
selenium.common.exceptions.WebDriverException: Message: Can't load the profile. Profile Dir: C:\Users\Gauss\AppData\Local\Temp\tmpkpjhnhpb If you specified a log_file in the FirefoxBinary constructor, check it for details.
Could someone please suggest a solution to this issue?
Note: This issue is on my office pc and the same code works fine on my home pc. So some issue with Firefox maybe.
Based on your comments, it looks like the version of Firefox you have is not yet supported by python driver. Currently they support up to v. 38, you have 44.

Categories