My objective is to take a value (in my case a shipment tracking #) and input it into a tracking field on a website using selenium. I am unable to input the value and get the following error message:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Users/GUVA/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.17track.net/en")
#data = df.values[1] # grabbing one tracking number from an excel file
# click "agree" to close window
python_button = driver.find_elements_by_xpath('//*[#id="modal-gdpr"]/div/div/div[3]/button')[0]
python_button.click()
# enter tracking number into text box
que=driver.find_element_by_id('//input[#id="jsTrackBox"]')
que.send_keys("data")
Traceback (most recent call last):
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/Track Shipment.py", line 26, in <module>
que=driver.find_element_by_id('//input[#id="jsTrackBox"]')
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/GUVA/PycharmProjects/Shipment_Tracking/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified
(Session info: chrome=83.0.4103.61)
Any suggestions please?
//input[#id="jsTrackBox"] is an xpath. So you need to fetch the element by using find_element_by_xpath method(where you are currently using find_element_by_id method).
Your code should be like:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Users/GUVA/Downloads/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.17track.net/en")
#data = df.values[1] # grabbing one tracking number from an excel file
# click "agree" to close window
python_button = driver.find_elements_by_xpath('//*[#id="modal-gdpr"]/div/div/div[3]/button')[0]
python_button.click()
# enter tracking number into text box
que=driver.find_element_by_xpath("//div[#id='jsTrackBox']//div[#class='CodeMirror-scroll']")
que.click()
que.send_keys("data")
Related
I am trying to scrape data from this File-tuning dynamic website which is loading data through javascript (ajax) requests.
what is want to do is that It selects cars from type and then selects make, model, engine iteratively, and then I want to scrape data for each make, model, and engine.
Here is the code I write to select cars from type
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
PATH = "C:\SeleniumDrivers\geckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.get("https://file-tuning.com/chiptuning")
type_element_select = driver.find_element_by_id("type")
action = ActionChains(driver)
action.move_to_element(type_element_select)
action.click(type_element_select)
action.perform()
action.move_to_element(Select(type_element_select).select_by_value("cars"))
action.click(Select(type_element_select).select_by_value("cars"))
action.perform()
The error I get:
Traceback (most recent call last):
File "D:\Python\selenium\test.py", line 27, in <module>
action.move_to_element(Select(type_element_select).select_by_value("cars"))
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py", line 82, in select_by_value
self._setSelected(opt)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\support\select.py", line 212, in _setSelected
option.click()
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Umair\AppData\Local\Programs\Python\Python39\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 <option> could not be scrolled into view
I also try this
types_element = driver.find_element_by_id("type")
types_object = Select(types_element)
types_object.select_by_visible_text("Cars")
but it also gives me the same exception.
how I can scrape through this site?
already have seen and tried other StackOverflow related question but didn't work out for me.
You need to use browser in full screen mode :
driver.maximize_window()
so that all web elements would be visible to Selenium.
Sample code :
PATH = "C:\SeleniumDrivers\geckodriver.exe"
driver = webdriver.Firefox(executable_path=PATH)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://file-tuning.com/chiptuning")
wait = WebDriverWait(driver, 10)
type_element_select = driver.find_element_by_id("type")
action = ActionChains(driver)
action.move_to_element(type_element_select)
action.click(type_element_select)
action.perform()
action.move_to_element(Select(type_element_select).select_by_value("cars"))
action.click(Select(type_element_select).select_by_value("cars"))
action.perform()
I've error in Python Selenium. I'm trying to download all songs with Selenium, but there is some error. Here is code:
from selenium import webdriver
import time
driver = webdriver.Chrome('/home/tigran/Documents/chromedriver/chromedriver')
url = 'https://sefon.pro/genres/shanson/top/'
driver.get(url)
songs = driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/a')
for song in songs:
song.click()
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[3]/div[1]/a[2]').click()
time.sleep(8)
driver.get(url)
time.sleep(5)
And here is error:
Traceback (most recent call last):
File "test.py", line 13, in <module>
song.click()
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/home/tigran/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/tigran/.local/lib/python3.8/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
(Session info: chrome=90.0.4430.72)
Any ideas why error comes?
Before your loop try to add try:
Sometimes the document is not attached, so it gives an error
Here's link selenium while loop not working. You need to locate elements in loop.
Final code:
from selenium import webdriver
import time
driver = webdriver.Chrome('/home/tigran/Documents/chromedriver/chromedriver')
url = 'https://sefon.pro/genres/shanson/top/'
driver.get(url)
size = len(driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/a'))
for i in range(0, size):
songs = driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div[1]/div[3]/div/div[3]/div[2]/a')
songs[i].click()
time.sleep(5)
driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[3]/div[1]/a[2]').click()
time.sleep(8)
driver.execute_script("window.history.go(-1)")
time.sleep(5)
Hello I would like to ask if there is any way to refresh source code of site without refreshing page. The problem is when I load page http://107.170.101.241:8080/getTableColumn/ and put there some information - you can see in my code below and then click Analyse there is displayed new textarea. I want to get text from this textarea but I cant because source code is “old” and xpath cant find it. Last line of code is what I want to print to console. I tried time.sleep etc. and nothing helped.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import time
chromedriver = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("http://107.170.101.241:8080/getTableColumn/")
time.sleep(1)
datab = driver.find_element_by_xpath("//select[#name='dbVendor']")
database = Select(datab)
database.select_by_visible_text("Sybase")
datab2 = driver.find_element_by_xpath("//select[#name='options']")
database2 = Select(datab2)
database2.select_by_visible_text("Show By SQL Clause")
txt = driver.find_element_by_xpath("//textarea[#name='sql']")
txt.clear()
txt.send_keys("select trd.M_NB as 'Trade_number' from CRD_TRADE_REP trd")
txt1 = driver.find_element_by_xpath("//textarea[#name='metadata']")
txt1.clear()
txt1.send_keys("CRD_TRADE_REP, M_NB")
analyze = driver.find_element_by_xpath("//input[#type='submit']")
analyze.send_keys("")
analyze.send_keys(Keys.RETURN)
#cant find this textarea below
out = driver.find_element_by_xpath("//textarea[#name='outputText']")
Here is Traceback:
Traceback (most recent call last):
File "/Users/martinkubicka/Documents/fiverrgde.py", line 32, in <module>
out = driver.find_element_by_xpath("//textarea[#name='outputText']")
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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":"//textarea[#name='outputText']"}
(Session info: chrome=84.0.4147.135)
In devtools, If you scroll up from your element, you can see your output textarea is nested in:
<iframe name="result" id="result" style="height: 180px; width: 800px;" scrolling="no" frameborder="0">
These need extra handling in selenium.
Try this at the end of your script:
#Get the frame
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='result']")))
#wait for your object to be ready - i use clickable as i like it
out = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//textarea[#name='outputText']")))
print(out.text)
#do stuff your stuff to the "out" element here
#when ready to go back to the main page content (not the iframe)
driver.switch_to_default_content()
When i run your code with that addition i get the output:
Tables: tetSelect CRD_TRADE_REP(1,40) Columns: selectList
CRD_TRADE_REP.M_NB(1,12)
This question already has answers here:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable when clicking on an element using Selenium Python
(6 answers)
Closed 2 years ago.
I want to automate through python using selenium and I need help.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
Sergo=['Sergo']
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/")
print(driver.title)
search = driver.find_element_by_xpath('//*[#id="search"]')
search.send_keys(Sergo)
time.sleep(5)
driver.quit()
Here is the error:
Traceback (most recent call last):
File "c:/Users/user/Desktop/bot/automation.py", line 11, in <module>
search.send_keys(Sergo)
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT,
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\user\Desktop\bot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\user\Desktop\bot\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=83.0.4103.116)
Your xpath is not unique
Use this xpath:
search = driver.find_element_by_xpath('//input[#id='search']')
This is because you don't have the Chromedriver.
Click on the 3 dots at the right-top in your Chrome browser
Click on About Chrome/Chromium
Check the version mentioned in light gray
Go to the ChromeDriver [downloads page]
(https://chromedriver.chromium.org/downloads).
Download the correct version as per your browser version.
Copy the path to ChromeDriver.exe
As an argument,pass executable_path=r'path/to/chromedriver.exe'
in driver = webdriver.Chrome().
For example, mine is:
driver = webdriver.Chrome(executable_path=r'C:\Users\dell\Libs\chromedriver.exe')
I'm building a whatsapp automation script using python selenium. I'm unable to find the message text area to send message.
My code :
from selenium import webdriver
import pandas as pd
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import os
import mysql.connector
driver = webdriver.Chrome('/Users/evilslab/Downloads/chromedriver 3')
driver.get('https://web.whatsapp.com/send?phone=0097155960&text&source&data&app_absent')
time.sleep(10)
text = "Hello testing"
inp_xpath = '//*[#id="main"]/footer/div[1]/div[2]'
input_box = driver.find_element_by_xpath(inp_xpath)
time.sleep(2)
input_box.send_keys(text + Keys.ENTER)
time.sleep(2)
The terminal is :
Traceback (most recent call last):
File "/Users/evilslab/Documents/Websites/www.futurepoint.dev.cc/dobuyme/classy/whatsapp-selenium.py", line 18, in <module>
input_box = driver.find_element_by_xpath(inp_xpath)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/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":"//*[#id="main"]/footer/div[1]/div[2]"}
(Session info: chrome=81.0.4044.138)
May be your xpath is wrong. When I inspect and get xpath I get //*[#id="main"]/footer/div[1]/div[2]/div/div[2]
i guess there is a problem with the xpath
find how to get specific xpath from here Is there a way to get the XPath in Google Chrome? , sometimes whatsapp change their class name