New to Python Selenium.
I am trying to create an script to login to my home router and press the button restart.
Running to error, when trying to login to the router, can some on guide on my mistake here.
below is the code and also attaching the .screenshot
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
driver_service = Service(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
PASSWORD = 'testtes'
login_page = 'http://192.168.2.1/login.html'
driver.get(login_page)
driver.find_element_by_xpath("//input[#placeholder='Password']").send_keys(PASSWORD)
Below is the error I am getting.
Traceback (most recent call last):
File "C:\Users\admin\Desktop\pyhton\index.py", line 14, in
driver.find_element_by_xpath("//input[#placeholder='Password']").send_keys(PASSWORD)
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'
getting this error now.
Traceback (most recent call last):
File "C:\Users\admin\Desktop\pyhton\index.py", line 13, in
driver.find_element(By.XPATH, "//input[#placeholder='Password']").send_keys(PASSWORD)
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 856, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 429, in execute
self.error_handler.check_response(response)
File "C:\Python310\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, 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[#placeholder='Password']"}
Probably you are using Selenium 4. if so, find_element_by_xpath and all the others find_element_by_* methods are not supported by Selenium 4, you have to use the new syntax and add an essential import, as following:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
driver_service = Service(executable_path="C:\Program Files (x86)\chromedriver.exe")
driver = webdriver.Chrome(service=driver_service)
PASSWORD = 'testtes'
login_page = 'http://192.168.2.1/login.html'
driver.get(login_page)
driver.find_element(By.XPATH, "//input[#placeholder='Password']").send_keys(PASSWORD)
Try this:
from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, "//input[#placeholder='Password']").send_keys(PASSWORD)
Related
I want to organize my urls for the testing project, so I tried to create a separate class or dictionary to store urls, but whenever I tried to call a "driver.get(pages.url)" method I get an error. Although if you pass the url from a variable or a string directly, everything works.
I can't understand where is the problem. Could someone help?
The code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
class Pages:
basic_url:str = 'google.com'
translate:str = 'translate.' + basic_url
maps:str = 'www.' + basic_url + '/maps'
pages = Pages()
print(pages.translate)
print(type(pages.translate))
chrome_options = Options()
chrome_options.add_experimental_option("detach", True)
driver = webdriver.Chrome(service=Service(executable_path=ChromeDriverManager().install()),options=chrome_options)
wait = WebDriverWait(driver, 60)
driver.maximize_window()
driver.get(pages.translate)
The error:
'Traceback (most recent call last):
File "E:\Projects\Python\portal_UI_testing_automation\ptn.py", line 23, in <module>
driver.get(pages.translate)
File "E:\portal_UI_testing_automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in get
self.execute(Command.GET, {'url': url})
File "E:\portal_UI_testing_automation\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
self.error_handler.check_response(response)
File "E:\portal_UI_testing_automation\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=106.0.5249.119)'
Print your url before passing it to driver.get to make sure its valid.
As some of the comments suggest try adding http/https.
I think you are missing https:// in the URL
This is the correct answer. Thanks Sighil, and others.
I'm trying to get access to this page "fullcollege" with a bot I'm making for students. The problem is that I can't even select an element from it, and this error shows up. I have recently tested my code with another webpages like instagram and everything works perfectly. Anyone knows what can I do to solve this? Thanks in advance.
My code:
from time import sleep
from selenium import webdriver
browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.fullcollege.cl/fullcollege/')
sleep(5)
username_input = browser.find_element_by_xpath("//*[#id='textfield-3610-inputEl']")
password_input = browser.find_element_by_xpath("//*[#id='textfield-3611-inputEl']")
username_input.send_keys("username")
password_input.send_keys("password")
sleep(5)
browser.close()
The error:
Traceback (most recent call last):
File "c:\Users\marti\OneDrive\Escritorio\woo\DiscordBot\BetterCollege\tester.py", line 11, in <module>
username_input = browser.find_element_by_xpath("//*[#id='textfield-3610-inputEl']")
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\marti\AppData\Local\Programs\Python\Python38\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: //*[#id='textfield-3610-inputEl']
The username and password field is inside and iframe you need to switch it first.
browser.get('https://www.fullcollege.cl/fullcollege/')
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#logFrame")))
sleep(5)
username_input = browser.find_element_by_xpath("//input[#id='textfield-3610-inputEl']")
password_input = browser.find_element_by_xpath("//input[#id='textfield-3611-inputEl']")
username_input.send_keys("username")
password_input.send_keys("password")
import below libraries as well.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
This question already has answers here:
Can't send keys selenium webdriver python
(6 answers)
Closed 4 years ago.
I've written a simple function. The purpose of go_to_url so we can set a max load time for a page. If a page does not load within the timeout_limit, then we will try loading the page again. However, I am getting an error if the page does fail to load within the time frame and goes into the exception.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
def go_to_url(driver, url, timeout_limit):
while True:
try:
driver.set_page_load_timeout(timeout_limit)
driver.get(url)
break;
except TimeoutException:
driver.send_keys(Keys.ESCAPE)
print('Failed to load, reloading page')
options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)
go_to_url(driver, "http://www.emba.com.tw/", 4)
Error:
Traceback (most recent call last):
File "test.py", line 15, in go_to_url
driver.get(url)
File "C:\Users\pc\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "C:\Users\pc\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\pc\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.TimeoutException: Message: timeout
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f743
87),platform=Windows NT 6.3.9600 x86_64)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 25, in <module>
go_to_url(driver, "http://www.emba.com.tw/", 4)
File "test.py", line 18, in go_to_url
driver.send_keys(Keys.ESCAPE)
AttributeError: 'WebDriver' object has no attribute 'send_keys'
Your code block was near perfect.
This error message...
AttributeError: 'WebDriver' object has no attribute 'send_keys'
...implies that the WebDriver implementation has no attribute as send_keys
selenium.webdriver.remote.webelement which reprsents a DOM element contains the method send_keys(*value) which defined as:
send_keys(*value)
Simulates typing into the element.
So an appropiate way to invoke send_keys() must be associated on an element as follows:
driver.find_element_by_css_selector("input.string").send_keys("Yuri")
So im pretty new to selenium and im following the docs to make some bots,
but when i try to login into social media networks (twitter/instagram) it doesnt send the strokes.
Code:
#!usr/bin/env python3
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.get("https://www.instagram.com/accounts/login/")
elem = browser.find_element_by_name("username")
elem.send_keys('Laptops' + Keys.RETURN)
time.sleep(4)
browser.quit()
i've tried it by using- browser.get_element_by_name/class/xpath but nothing seemed to work.
Error code:
Traceback (most recent call last):
File "ig.py", line 50, in <module>
login(driver)
File "ig.py", line 15, in login
driver.find_element_by_xpath("//div/input[#name='username']").send_keys(username)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 365, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 843, in find_element
'value': value})['value']
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //div/input[#name='username']
os=ubuntu17
driver=firefox/geckodriver
python3.6
selenium3.6
I know this code shouldnt work bc you need a password and a username, but it doesnt even execute the send_keys code because of the error on the line above tHAT
I've had some problems with Firefox driver on linux too.
I copied your code and only changed the driver to Chrome and it worked.
You can get the Chrome driver here:
http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.chrome.webdriver
Try adding a wait-for presence condition. It may however require a EC.element_to_be_clickable, if the element is disabled until some check is performed.
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
...
usernamefield = WebDriverWait(self.driver, 10)\
.until(EC.presence_of_element_located((By.NAME, 'username')))
usernamefield.send_keys("Laptops")
passwordfield = WebDriverWait(...
Thats my code, I was changing, fixing, trying another modules, but still can not get a respond with my div text extracted.
import selenium
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')
WebElement = driver.findElement(By.xpath("//div[#class='main']"));
webElement.getText();
I was trying with bs4 package but there is a big problem, becouse the data what i want is possible to get only when i am loged in on the website, and in bs4 respond was like from guest account, without login in.
Here is a Traceback what i got usint that code with Selenium:
Traceback (most recent call last):
File "D:/Python27/get text value div.py", line 8, in <module>
WebElement = driver.findElement(By.xpath("//div[#class='main']"));
AttributeError: 'WebDriver' object has no attribute 'findElement'
Aftter a small fix, I am using this:
import selenium
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')
main_text = element.text
The respond after print element.text in shell is:
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
element.text
File "D:\Python27\lib\selenium\webdriver\remote\webelement.py", line 50, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "D:\Python27\lib\selenium\webdriver\remote\webelement.py", line 228, in _execute
return self._parent.execute(command, params)
File "D:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "D:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 152, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: u'\'[JavaScript Error: "a is null" {file: "file:///c:/dokume~1/tomek/lokale%20einstellungen/temp/tmpupvgr2/extensions/fxdriver#googlecode.com/components/command_processor.js" line: 7623}]\' when calling method: [nsICommandProcessor::execute]'
Where did you get the following from? Looks like Java to me.
WebElement = driver.findElement(By.xpath("//div[#class='main']"));
webElement.getText();
Try:
import selenium
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.helloworld.com/')
element = driver.find_element_by_id('main')
print element.text #There's no text under div main, what would you expect?
footer = driver.find_element_by_id('footer')
print footer.text
# Should print out "Copyright ©2013 helloworld.com. All Rights Reserved. About Us | Privacy Policy "
If the information you want is inside a div with an id of botloc then you need to grab that element.
import selenium
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.helloworld.com/')
bot_location = driver.find_element_by_id('botloc').text
print bot_location