Selenium Python: Expected "handle" to be a string, got [object Undefined] undefined - python

I use Firefox. I click a button but the page opens as a new tab and selenium stays focused on the original page with the button. I'm trying to switch and get the new url.
time.sleep(4)
second_driver.switch_to.window(second_driver.window_handles[1])
print(second_driver.current_url)
I get:
selenium.common.exceptions.InvalidArgumentException: Message: Expected "handle" to be a string, got [object Undefined] undefined
I also tried the following but it didn't have an effect. I still got the url of the old page.
second_driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

This was a Geckodriver bug. It was resolved on the 0.27.0 version release:
https://github.com/mozilla/geckodriver/releases
I have used to experience the same issue with the 0.24.0 version and then I have upgraded the Geckodriver to this version, and then the issue stopped happening.
However, I recommend using always the latest version of Geckodriver. I have checked with the 0.29.0 version and the bug is also gone.

Related

'WebDriver' object has no attribute 'find_element_by_link_text' - Selenium script suddenly stopped working

This is a weird issue I have ran into and I can't find any solution for this across the internet. I was using selenium in google colab to scrape a website and my code was working completely fine. I woke up the next day and ran the code again without changing a single line and don't know how/why my code starting giving me this error, AttributeError: 'WebDriver' object has no attribute 'find_element_by_link_text'. Same for find_element_by_class_name and id etc. I then rechecked a previously working script just to confirm and that gave me the same error too. I am confused about what happened suddenly and the scripts started giving me these errors.
How do I solve this? What am I doing wrong here?
!pip install selenium
!apt-get update
!apt install chromium-chromedriver
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=chrome_options)
driver.get("https://petrowiki.spe.org/PetroWiki")
driver.title #this line is returning the correct title value, code is able to access the url
peh = driver.find_element_by_link_text('Pet. Eng. Handbook')
peh.click()
Selenium just removed that method in version 4.3.0. See the CHANGES: https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES
Selenium 4.3.0
* Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
* Deprecated Opera support has been removed (#10630)
* Fully upgraded from python 2x to 3.7 syntax and features (#10647)
* Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
* Better support for co-operative multi inheritance by utilising super() throughout
* Improved type hints throughout
You now need to use:
driver.find_element("link text", "Pet. Eng. Handbook")
For improved reliability, you should consider using WebDriverWait in combination with element_to_be_clickable.

EDGE VERSION WON'T WORK with seleniumWebdriver all of a sudden

Using selenium webdriver with Edge browser. Worked fine until last night/morning. Errors are:
selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of MSEdgeDriver only supports MSEdge version 96
Current browser version is 98.whatever. It says the application was modified on 2/3 of this year, which I didn't consent to as far as I can remember.
Why did that catch? Did it automatically install an update without letting me know? Why would that be?
In any case, TLDR, how do I fix this?! and which does Selenium dislike: my edgeDriver, or Selenium being out of date? should I REVERT EdgeDriver by finding an older version (how?), or would it be best to update Selenium somehow?

How do I solve this Selenium.common.exceptions.WebDriverException error?

I got an WebDriverException error, saying that chromedriver.exe needs to be in path, but as you see in the second picture, I have already done that. I am also having trouble figuring out the "unknown error: DevToolsActivePort file doesn't exist"). Attached is the error. Right now I am using this website (https://christophegaron.com/scraping-linkedin-posts-with-selenium-and-beautiful-soup/) to download Chrome Driver and Selenium to my laptop (I already have BeautifulSoup on my laptop so I don't need that one). Also, I have both Ubuntu and Linux, so I am open to using either to solve my issue. I am also using Python 3.8.5, Ubuntu 20.04.2, Chrome version 91, and Linux version 4.4.0.
Also if anyone has any tips regarding how to scrape LinkedIn posts that would be amazing!
picture of the errors/exceptions
picture of code
Thanks for your help! I found that my error was that I was using Ubuntu on a Linux machine (as a VM), when I should have just ran the Chromium program on a natural machine.

python selenium edge webdriver version not supported

I am just getting started with Selenium. Right now I'm using Python. I tried this:
from selenium import webdriver
driver = webdriver.Edge()
and got the error message:
WebDriverException: Message: 'MicrosoftWebDriver.exe' executable needs to be in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687
So I followed the link and downloaded the latest version, but got a new error:
driver = webdriver.Edge('edgedriver_win64\msedgedriver.exe')
SessionNotCreatedException: Message: session not created: This version of MSEdgeDriver only supports MSEdge version 90
Current browser version is 81.0.416.77 with binary path C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
Now I don't know what to do. The earlies version for the webdrvier found from the download link is 88.
Update edge browser or use a previous version of driver
You can download previous version from:
https://msedgewebdriverstorage.z22.web.core.windows.net/
Click next page till you get the version you want ,

Anaconda Selenium issues with webdriver

I have been struggling to figure out why I keep getting errors trying to use selenium. I'm using a local install of anaconda3 on my /home/user unix drive at the company I work for. I already pip installed selenium, seemingly without issue, but when I try the following:
from selenium import webdriver
driver = webdriver.Firefox()
it fails with the following message:
WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line
I've tried downloading the most current chromedriver and trying with that, I've tried installing another gecko-driver, I've tried all kinds of things. But nothing is working. I'm happy to provide any amount of additional information, I just want to get this off the ground at some point...
Thank you!
from selenium import webdriver
path = r'C:\yourgeckodriverpath\geckodriver.exe'
driver = webdriver.Firefox(executable_path=path)
Alright, through a combination of the responses to this question, I have figured out what (I think) went wrong. I was using a linux anaconda install on my company's servers, which (I believe) meant my python had no access to a browser driver. The solution was sadly to install anaconda locally, manually download/unzip/install selenium and geckodriver, and then make sure I pass the whole "executable_path=path" parameter to the Firefox method. This didn't work for Chrome for some reason, which I'll assume has something to do with the unchangeable security specifications on my work machine. If any part of this doesn't sound right, feel free to chime in and shed more light on the issue. Thanks!

Categories