Unable to store a function in a variable in Selenium - python

Okay so I was writing my first selenium code which is:
browser.get('https://www.jetbrains.com/pycharm/')
browser.find_element_by_link_text('Download')
<selenium.webdriver.remote.webelement.WebElement (session="69f43bf4a5e98baf24ad3746dd01b514", element="0fa96685-d871-464f-aaaa-af02238a5f08")>
I wanted to simply go to the pycharm website and click on downloads, which worked fine.
But when I tried to store the previous function in a variable like so:
elem = browser.find_element_by_link_text('Download')
I got all these errors:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchWindowException: Message: no such window: window was already closed
(Session info: chrome=87.0.4280.66)
I have no idea what they mean or what ive done wrong. Help much appreciated.
PS: I am using pycharm and selenium chromedriver if thats helpful.

No issues in this.
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
browser.get('https://www.jetbrains.com/pycharm/')
element = browser.find_element_by_link_text('Download')
element.click()

Related

Selenium Python Invalid Column Error message

Here is the code which I use to open the new tab using Selenium Python,
self.driver.execute_script("window.open(arguments[0])", self.listing_url)
self.driver.switch_to.window(self.driver.window_handles[1])
time.sleep(10)
and it works perfectly. But I am planning to use WebDriverWait instead of time.sleep
self.driver.execute_script("window.open(arguments[0])", self.listing_url)
self.driver.switch_to.window(self.driver.window_handles[1])
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[#class="honbun"]')))
and when I am using it, it's throwing an error. Here is the error log:
Traceback (most recent call last):
File "D:\tender_scraper\engine\Spider.py", line 308, in parse_index_page
index_handle = self.load_listing_page(listing, i, index_listing)
File "D:\tender_scraper\spiders\gunmapref_listing_spider.py", line 298, in load_listing_page
WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[#class="honbun"]')))
File "D:\scraper\lib\site-packages\selenium\webdriver\support\wait.py", line 78, in until
value = method(self._driver)
File "D:\scraper\lib\site-packages\selenium\webdriver\support\expected_conditions.py", line 64, in _predicate
return driver.find_element(*locator)
File "D:\scraper\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1251, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "D:\scraper\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
self.error_handler.check_response(response)
File "D:\scraper\lib\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: missing or invalid columnNumber
(Session info: headless chrome=104.0.5112.48)
Any idea what's wrong with it & how to solve it? Previously, I used chrome 103 and changed it to 104 but still got the same error. It seems the error only occurs when changing into the new tab.

Unable to display website on chrome when ran the program

I had recently started learning automation with python using selenium. I had installed chromedriver but when i run the code chrome displays no output. My code:
from selenium import webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("www.wikipedia.com")
Output
Output :
C:\Users\Admin\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/untitled/1.py
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/untitled/1.py", line 5, in <module>
driver.get("www.wikipedia.com")
File "C:\Users\Admin\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\Admin\PycharmProjects\untitled\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Admin\PycharmProjects\untitled\venv\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
(Session info: chrome=88.0.4324.96)
Please help as I'm unable to proceed.
Thanks!
URL should contain scheme. Simply replace
"www.wikipedia.com"
with
"https://www.wikipedia.com"

Python Selenium-Debug a Simple Code with YouTube

I followed this tutorial for Selenium with Python:
So I did the same as the tutorial:
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.youtube.com/')
searchbox = driver.find_element_by_xpath('//*[#id="search-input"]')
searchbox.send_keys('cooking')
But I get this error:
Traceback (most recent call last):
File "C:/ANGUS/DATA_ANALYSIS/WEBSCRAPING/TextXXX.py", line 7, in <module>
searchbox.send_keys('cooking')
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Angus\PycharmProjects\Test\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=81.0.4044.138)
How can I fix this?
Another way to do that in order not to use the search is by inserting the query itself.
Code:
your_query = "cooking"
driver.get("https://www.youtube.com/results?search_query=" + str(your_query))
time.sleep(5)
In this way, it is like you are going immediately in the output that you want after searching a specific query of yours. So, let's say that instead of typing cooking, you can .get() to the URL you want by adding your query into this URL.

Python selenium send_keys not working in chrome

I'm new to python coding. And I'm trying to play around with python and Selenium in chrome. And I have this code where I'm trying to move a webpage up and down. But it isn't working.
body_elem = browser.find_element_by_tag_name('body')
for __ in range(3):
body_elem.send_keys(Keys.END)
sleep(2)
body_elem.send_keys(Keys.HOME)
sleep(2)
All I get is this long error message:
Traceback (most recent call last):
File "selenium_test.py", line 34, in <module>
body_elem.send_keys(Keys.HOME)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
Anybody got an idea of whats is wrong?
This is most likely because of the fact that the DOM tree or the body element signature itself changed after the first "down" move. One straightforward way to approach the problem would be to "re-find" the element constantly:
for _ in range(3):
browser.find_element_by_tag_name('body').send_keys(Keys.END)
sleep(2)
browser.find_element_by_tag_name('body').send_keys(Keys.HOME)
sleep(2)

Python Splinter issue

I just installed Splinter, input simple code like below but have error. Can someone help me on this? Thanks.
from splinter import Browser
b=Browser()
url = "http://www.google.com"
b.visit(url)
Error messages:
Traceback (most recent call last): File "", line 1, in
File
"/usr/local/lib/python2.7/dist-packages/splinter/driver/webdriver/init.py",
line 184, in visit
self.driver.get(url) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 245, in get
self.execute(Command.GET, {'url': url}) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py",
line 233, in execute
self.error_handler.check_response(response) File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py",
line 194, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: Target URL
www.google.ca is not well-formed. Stacktrace:
at FirefoxDriver.prototype.get (file:///tmp/tmpsIlz0H/extensions/fxdriver#googlecode.com/components/driver-component.js:10636)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpsIlz0H/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpsIlz0H/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpsIlz0H/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
Try this instead:
from splinter import Browser
with Browser() as b:
b.visit("http://www.google.com")
Docs here: https://splinter.readthedocs.org/en/latest/#sample-code
The method above handles quitting the browser. Your method, also in the docs, should work (it works for me). Try it again, I've noticed I get almost the same error as yours, if my url is "www.google.com". Make sure you have "http://" part.
selenium.common.exceptions.WebDriverException: Message: Target URL
www.google.com is not well-formed

Categories

Resources