Selenium Timout on RaspberryPi at Expected Condition - python

Task
I want to perform a web task using Selenium, geckodriver (ARM build) and Iceweasel on my RaspberryPi 3. The code runs seamlessly on my development machine (Ubuntu, Intel, 64bit), of course using the suitable geckodriver build. The code also prooved successful inside a dockerized container. However, when executing on the RaspberryPi, I get the stated timeout error.
Code
The website is opened in a new window, hence I am switching to the second in window.handles.
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from helpers.automation_helper import *
to_website.click()
el = WebDriverWait(driver, 20).until(
EC.number_of_windows_to_be(2)
)
driver.switch_to.window(driver.window_handles[1])
while get_events_left(driver):
...
In the opened page, I expect edit buttons (= my events) which I intend to return as soon as they are visible. An empty array shall be returned if no events are available, so causing the while loop not to be executed. The expected condition is where the error occurs on the RaspberryPi.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
def get_events_left(driver):
WebDriverWait(driver, 20).until(
EC.visibility_of_all_elements_located((By.XPATH, "//button[contains(#id, 'protocolEvent')]"))
)
return driver.find_elements_by_xpath("//button[./span/span/svg-icon[#name='edit']]")
Error
Traceback (most recent call last):
File "app.py", line 61, in <module>
while get_events_left(driver):
File "/path/to/app/helpers/automation_helper.py", line 39, in get_events_left
EC.visibility_of_all_elements_located((By.XPATH, "//*[contains(#id,'protocolEvent')]"))
File "/path/to/app/venv/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Related

Unable to find element by XPATH using Python and Selenium

I'm very newbie in Python and I'm trying to make login on Dell OpenManage webpage below by using Selenium:
Dell OpenManage
I already tried too many ways to locate the xpath of the Username box (/html/body/div1/div[2]/div/div/div1/div[6]/form/table/tbody/tr[2]/td1/input), but without success.
In the latest test I tried to wait the element be loaded to use send_keys and I received the error message below:
Traceback (most recent call last):
File "C:\LEARN\PY\RepositoryGitHub\Ancora-Automations\Ancora-Automations\ServersCheck.py", line 18, in
wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div1/div[2]/div/div/div1/div[6]/form/table/tbody/tr[2]/td1/input"))).send_keys('ezequiel.ferreira')
File "C:\Users\ezequiel.ferreira\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
My code is:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import tkinter as tk
from tkinter import simpledialog
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get('https://ancorabk02:1311/OMSALogin?msgStatus=null')
wait = WebDriverWait(driver, 2)
# Clicking on advanced and continue buttons to proceed on a untrusted page
driver.find_element('xpath', '/html/body/div/div[2]/button[3]').click()
driver.find_element('xpath', '/html/body/div/div[3]/p[2]/a').click()
wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div/div/div[1]/div[6]/form/table/tbody/tr[2]/td[1]/input"))).send_keys('ezequiel.ferreira')
Could you please help me to understand with this issue?
Thanks!

Locating a website element using selenium webdriver for automation task

I am working on a project using selenium webdriver which requires me to locate an element and click on it. The program starts by entering a website , clicking the searchbar , typing in a preentered string and clicking enter , up to this point everything is successful. The next thing I want it to do is find the first result of the search and click on it. This part I am having trouble with. I have successfully located all elements up to this point but i cant locate this one as an error pops up. Here is my code:
from selenium import webdriver
import time
trackname = input("Track Name: ")
driver = webdriver.Chrome('D:\WebDrivers\chromedriver.exe')
driver.get('https://music.apple.com/us/artist/search/166949667')
time.sleep(2)
searchbox = driver.find_element_by_tag_name('input')
searchbox.send_keys(trackname)
from keyboard import press
press('enter')
time.sleep(2)
result = driver.find_element_by_id('search-list-lockup__description')
result.click()
I have tried locating the element other ways but it wont work , I am guessing that the issue is that after searching I have to tell it to search on that page but I am not sure. Here is the error:
Traceback (most recent call last):
File "D:\Python Projekti\iTunesDataFiller\iTunesDataFiller.py", line 18, in <module>
result = driver.find_element_by_id('search-list-lockup__description')
File "D:\Python App\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:\Python App\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "D:\Python App\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "D:\Python App\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="search-list-lockup__description"]"}
(Session info: chrome=89.0.4389.114)
Process finished with exit code 1
What do I do?
This could be due to the element you want to access not being available.
For example say you load the page, the element could not be visible to selenium. So basically you're trying to click on an invisible element.
I suggest using this template to make sure elements are loaded before accessing them.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
waitshort = WebDriverWait(driver,.5)
wait = WebDriverWait(driver, 20)
waitLonger = WebDriverWait(driver, 100)
visible = EC.visibility_of_element_located
driver = webdriver.Chrome(executable_path='path')
driver.get('link')
element_you_want_to_access = wait.until(visible((By.XPATH,'xpath')))

How can I make the selenium chromedriver make wait until it maximizes the window?

I've been messing around with python/selenium a bit, now I got to use the "try" method. I want the driver to wait until it maximized the window, and then go on with the code. At the beginning it just went on executing all scripts and the chrome window got maximized whilst executing it, which looked pretty weird.
Anyways I hope you can help me,
Have a good day y'all ;))
Ah, here's the code: (And the error message beneath it)
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(executable_path=r'C:\Users\Anyone\Desktop\Python\chromedriver.exe')
print("Automation Started, please hold on.")
driver.get("https://blablabla")
try:
recap = WebDriverWait(driver, 5).until(
EC.WebDriverException(driver.maximize_window())
)
finally:
driver.forward()
And here's the error message:
Traceback (most recent call last):
File "C:/Users/Anyone/PycharmProjects/blebleble/blablabla.py", line 17, in <module>
EC.WebDriverException(driver.maximize_window())
File "C:\Users\Anyone\PycharmProjects\blablabla\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
TypeError: 'WebDriverException' object is not callable
Don't wait for the Selenium driven ChromeDriver initiated google-chrome Browsing Context to open first and then maximize it.
Instead, add the argument start-maximized and configure the driver, so the Google Chrome Browsing Context opens up maximized using the solution below:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://blablabla")
After making driver object use the below code:-
driver.manage().window().maximize();
and remove the try/finally block.

Script can't find Element but Console can [Selenium] [Python]

I'm trying to write script to automate the process of downloading the instagram stories but I'm failing already when trying to log in.
I'm writing the code inside Pycharm. I just tried my usual approach to any problem. First, solve it with typing the commands out in the console and if it works writing the commands which worked inside the console down in a script. But here is the issue. The function which worked perfectly fine inside the python console fails inside the script.
I've noticed that my selenium was outdated but upgrading it didn't help ether. I also made a new project to test weather that made difference, which it didn't.
I've also tried skipping the first step inside the script and just opening the url to which I'm redirected. But the second commands failed as well.
When I create a new variable to store the output of the driver.find_element_by_link_text() in, it returns an empty list. This leads me to belive that somehow selenium is unable to search the contetns of the page.
I've also tried the same on Chrome and Safari. This also didn't work.
Here is the code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://instagram.com/")
#next command fails
driver.find_element_by_link_text("Melde dich an.").click()
#if the first command is skipped by entering in the url
#in driver.get(https://www.instagram.com/accounts/login/?source=auth_switcher)
#the following command fails as well.
driver.find_element_by_name("username").send_keys("HereIsTheUsername")
driver.find_element_by_name("password").send_keys("HereIsThePassword")
driver.find_element_by_name("password").send_keys(Keys.RETURN)
driver.close()
In the console these commands worked as mentioned,
Here is what I've entered into the console:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://instagram.com/")
driver.find_element_by_link_text("Melde dich an.").click()
#if it failed here would be an error message
element = driver.find_element_by_name("username")
With the script the error message is this:
Traceback (most recent call last): File
"/Users/alisot2000/PycharmProjects/Instagram downloader/venv/Main.py",
line 6, in
driver.find_element_by_link_text("Melde dich an.").click() File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 428, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text) File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 978, in find_element
'value': value})['value'] File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 321, in execute
self.error_handler.check_response(response) File "/Users/alisot2000/PycharmProjects/Instagram
downloader/venv/lib/python3.7/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: Melde dich an.
Issues you might be experiencing:
1. Synchronization Issue
For most automation tasks, there will be different loading times of web pages based on the processing power of the machine and how strong your internet connection is.
To solve this there are library import Waits from selenium that we can use.
Here is a sample below:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
2. Wrong language set in selenium profile
Selenium will use your locale in most cases when running automation scripts but in the case that you might want another language here is a sample code for FireFox.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
profile = webdriver.FirefoxProfile()
# switch out 'de' with another two character language code
profile.set_preference("intl.accept_languages",'de')
driver = webdriver.Firefox(firefox_profile=profile, executable_path='<insert_your_gecko_driver_path_here>')
driver.get("https://instagram.com/")
driver.close()
3. Working Code(Tested on Mojave 10.14.5)
Here is a diff of your code and the altered code: https://www.diffchecker.com/G0WWB4Ry
setup a virtualenv
pip install selenium
download geckodriver
set path to gecko driver in code
run script with success result
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# these two imports are for setting up firefox driver and options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
# import these three lines below if you are having synchronization issues
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
profile = webdriver.FirefoxProfile()
# here is where you need to set your language explicitly if its defaulting to an undesired language
# just replace the second parameter with your 2 character language code
# this line is not needed if your desired language is locale
profile.set_preference("intl.accept_languages",'de')
# throw in your path here <insert_your_gecko_driver_path_here>
driver = webdriver.Firefox(firefox_profile=profile, executable_path='<insert_your_gecko_driver_path_here>')
driver.get("https://instagram.com/")
# added these two lines below to solve synchronization issue
# element wasnt clickable until page finished loading
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Melde dich an.")))
#next command fails
driver.find_element_by_link_text("Melde dich an.").click()
#if the first command is skipped by entering in the url
#in driver.get(https://www.instagram.com/accounts/login/?source=auth_switcher)
#the following command fails as well.
driver.find_element_by_name("username").send_keys("HereIsTheUsername")
driver.find_element_by_name("password").send_keys("HereIsThePassword")
driver.find_element_by_name("password").send_keys(Keys.RETURN)
driver.close()
def ClickElementByName(name,driver):
while True:
try:
driver.find_element_by_name(name).click()
break
except:
sleep(1)
pass
Too long to wait the website run.
Replace ClickElementByName("username", driver)
driver.find_element_by_xpath('//input[#name="username"]').send_keys("HereIsTheUsername")
driver.find_element_by_xpath('//input[#name="password"]').send_keys("HereIsTheUsername")
driver.find_element_by_xpath('//div[text()="Log In"]').click()

Python Selenium Explicit WebDriverWait function only works with presence_of_element_located

I am trying to use Selenium WebDriverWait in Python to wait for items to load on a webpage , however, using any expected condition apart from presence_of_element_located seems to result in the error
selenium.common.exceptions.WebDriverException: Message: SyntaxError: missing ) in parenthetical
I thought it might be linked to the site I was trying against , however I get the same error on any site - see snippit below where I have replaced presence_of_element_located with visibility_of_element_located and am trying to confirm visibility of the search box on python.org.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
try:
element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME,"q")))
element.send_keys("pycon")
element.send_keys(Keys.RETURN)
finally:
driver.quit()
The Full stack trace is as below , Any help would be appreciated !
Traceback (most recent call last):
File "C:\dev\test.py", line 51, in <module>
element = WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.NAME,"q")))
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\support\expected_conditions.py", line 78, in __call__
return _element_if_visible(_find_element(driver, self.locator))
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\support\expected_conditions.py", line 98, in _element_if_visible
return element if element.is_displayed() == visibility else False
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\webelement.py", line 353, in is_displayed
self)
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 465, in execute_script
'args': converted_args})['value']
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Development\python\python35-32\lib\site-packages\selenium-3.0.0b3-py3.5.egg\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: SyntaxError: missing ) in parenthetical
Update - > After a few comments below i have done some testing on versions and browsers and this issue seems isolated to Python 3 and Firefox , the script works with Python 2.7 and works on both versions of python for Chrome webdriver .
These minor changes work for me.
visibility_of_element_located ===> presence_of_element_located
driver.quit() ===> driver.close()
See the following:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
try:
element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.NAME,"q")))
element.send_keys("pycon")
element.send_keys(Keys.RETURN)
finally:
driver.close()
Copy pasted the same code and it works.
Dint have enough repo to post comments so had to put it in answer section.

Categories