0

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?

1 Answer 1

1

//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")
Sign up to request clarification or add additional context in comments.

9 Comments

Thank You. I was able to fix the error but I am unable to input the string "data" into the box.
@Goutham i have put an extra line in the answer where i am first clicking on the field and then entering the value. Please pick that from the answer and let me know if it works.
Yes, I did include the additional line where we click the box but it still does not work. Maybe my id="jsTrackBox" is wrong?
@Goutham i have edited the xpath, please pick the updated answer.
How did you get the xpath? When I right click on the element, this is not the xpath that I get. Even the updated xpath does not seem to work. The "from selenium.webdriver.common.keys import Keys" is for some reason grayed out and maybe send.keys command is therefore not functioning
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.