Run selenium on pythoanywhere.com - python

I've wrote a code and works perfectly on my computer, then i decided to move it on a server cloud, and then I create an account on pythonanywhere.com
Then, after a lot of try I understand that chromedriver is not working over there, but just firefox, no big deal .
Then I did some test and works fine, but when I tried to use:
browser.find_element_by_link_text('Log in').click()
It doesn't work
well, my code is:`
from pyvirtualdisplay import Display
from selenium import webdriver
from time import sleep
with Display():
browser = webdriver.Firefox()
try:
browser.get('https://www.instagram.com')
print("get instagram.com ")
browser.implicitly_wait(60)
browser.find_element_by_link_text('Log in').click()
print("click log in")
sleep(10)
finally:
browser.quit()
the error is :
get instagram.com
Traceback (most recent call last):
File "/home/fobu36/.virtualenvs/prova_selenium.py", line 16, in <module>
browser.find_element_by_link_text('Log in').click()
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", l
ine 317, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", l
ine 752, in find_element
'value': value})['value']
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/webdriver.py", l
ine 236, in execute
self.error_handler.check_response(response)
File "/home/fobu36/.local/lib/python3.6/site-
packages/selenium/webdriver/remote/errorhandler.py"
, line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
element: {"method":"l
ink text","selector":"Log in"}
Stacktrace:
at FirefoxDriver.findElementInternal_
(file:///tmp/tmpbydkaxfk/extensions/fxdriver#googlecode.
com/components/driver-component.js:10770)
at fxdriver.Timer.setTimeout/<.notify
(file:///tmp/tmpbydkaxfk/extensions/fxdriver#googlecode.
com/components/driver-component.js:625)
seems like that it can't find the log in button, but i'm sure that in local it's working

Related

Selenium WebDriver.wait does not wait Python

I want to have Selenium wait until a webpage is loaded. I use Safari 14.1.2 on macOS 10.15.7
So I tried as below:
// login process and it successes and the following page shows up
login_submit.click()
// the page after the authorization, make selenium wait until ```hoge``` is clickable
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'hoge')))
This code does not work as expected. It fails with an error selenium.common.exceptions.NoSuchFrameException:. The error comes immediately, which seems there is no 10 s wait.
The stack trace is as below: (Paths are modified.)
Traceback (most recent call last):
File "/login.py", line 99, in <module>
login_schedule().quit()
File "/login.py", line 67, in login_schedule
wait.until(EC.presence_of_element_located((By.ID, 'HOGEHOGE')))
File "/selenium/webdriver/support/wait.py", line 71, in until
value = method(self._driver)
File "/selenium/webdriver/support/expected_conditions.py", line 64, in __call__
return _find_element(driver, self.locator)
File "/selenium/webdriver/support/expected_conditions.py", line 415, in _find_element
raise e
File "/selenium/webdriver/support/expected_conditions.py", line 411, in _find_element
return driver.find_element(*by)
File "/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message:
When I put time.sleep(10) instead as below,
import time
// login process and it successes and the following page shows up
login_submit.click()
time.sleep(10)
// the page after the authorization, make selenium wait until ```hoge``` is clickable
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'hoge')))
it works as expected.
Note that the last part can even be replaced
browser.find_element_by_xpath("//select[#id='hoge']")
What is wrong with my Python code?
try to use this function to wait until the element or something appears:
def wait_for(xpath):
while True:
try:
driver.find_element_by_xpath(xpath)
return True
except NoSuchElementException:
continue
do not forget
from selenium.common.exceptions import NoSuchElementException
you can use anything it is not important to be XPATH as a method.

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"

Unable to store a function in a variable in Selenium

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

I am trying to automate opening charts in Tradingview using Selenium with Python

from selenium import webdriver
import time
DRIVER = 'chromedriver'
driver = webdriver.Chrome(DRIVER)
driver.get("https://www.tradingview.com/#signin")
driver.find_element_by_name("username").send_keys("Username")
driver.find_element_by_name("password").send_keys("Password")
driver.find_element_by_xpath("//button[#type='submit']").click()
driver.maximize_window()
driver.find_element_by_link_text('Chart').click()
driver.find_element_by_class_name('input-3lfOzLDc').send_keys('SBIN')
time.sleep(20)
driver.quit()
Everything is working fine here but when I try to find element which needs to be change( the one sending SBIN , I get this error
Traceback (most recent call last):
File "tview.py", line 11, in <module>
driver.find_element_by_xpath("//input[#class='input-3lfOzLDc']").send_keys('SBIN')
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/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: no such element: Unable to locate element: {"method":"xpath","selector":"//input[#class='input-3lfOzLDc']"}
Can someone please help out
waiting a little bit longer works fine. I imported keys too because you had to clear that textbox
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
DRIVER = 'chromedriver'
driver = webdriver.Chrome(DRIVER)
driver.get("https://www.tradingview.com/#signin")
driver.find_element_by_name("username").send_keys("sibyl666")
driver.find_element_by_name("password").send_keys("417305628m")
driver.find_element_by_xpath("//button[#type='submit']").click()
driver.maximize_window()
driver.find_element_by_link_text('Chart').click()
time.sleep(5)
driver.find_element_by_class_name('input-3lfOzLDc').send_keys(Keys.CONTROL + "a")
driver.find_element_by_class_name('input-3lfOzLDc').send_keys(Keys.DELETE);
driver.find_element_by_class_name('input-3lfOzLDc').send_keys('SBIN')
time.sleep(20)
driver.quit()
this may be too late, but here it is what works for me. Basically, I use .clear() to get rid of the previous value and add some waiting times (depends on how fast the website loads on your machine).
ticker = driver.find_element_by_class_name('input-3lfOzLDc')
time.sleep(2)
ticker.clear()
time.sleep(2)
ticker.send_keys('SBIN')
time.sleep(2)
ticker.send_keys(Keys.RETURN)

Python Splinter cannot click button

I am running through the Splinter tutorial located here: http://splinter.readthedocs.io/en/latest/tutorial.html#
The code I am using:
from splinter import Browser
browser = Browser()
browser.visit('http://google.com')
browser.fill('q', 'chicago pizza')
button = browser.find_by_name('btnG')
button.click()
Every time I try to get my code to click the search I get the following error:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
button.click()
File "C:\Python27\lib\site-packages\splinter\driver\webdriver\__init__.py", line 546, in click
self._element.click()
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
ElementNotInteractableException: Message:
I have used browser.find_by_id, browser.find_by_tag, browser.find_by_text and none have worked, all giving me the same error.
I am running python 2.7.8 Firefox v 54.0.1 (32-bit) and have selenium installed.
Any idea of how to fix this? I think it may have to do with my geckodriver.
I would like to solve the problem and not have to work around it by say switching web browsers.
Everything worked fine except you have entered wrong name of button.
Here is the working code
from splinter import Browser
browser = Browser()
browser.visit('http://google.com')
browser.fill('q', 'chicago pizza')
button = browser.find_by_name('btnK')
button.click()

Categories

Resources