Hey SO community once again, im running into a small problem while trying to automate something, So I'm watching this YouTube tutorial and I know no ones going to go through the hassle to check what the guy is doing for reference at approximately 5:50 he copies the xpath and pastes it on the side, however when I do it it gets underlined in red. here's what I did
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/Users/name/Desktop/Selenium/chromedriver')
url = 'https://healthscreening.schools.nyc/?type=G'
driver.get(url)
//*[@id="guest_last_name"]
//*[@id="guest_email"]
//*[@id="btnDailyScreeningSubmit"]/button
driver.find_element_by_xpath('')...
The /*[@id=""] part is all underlined in red does anyone know the cause of the problem is? Thanks
CodePudding user response:
You didn't complete the video, did you?
The reason you're getting red underlines is because your IDE has detected them as invalid strings and they need to be enclosed in quotes (single quotes as there are double quotes in the string already).
Moreover, if you watch the video a little further, you'd see that he just copied it because he doesn't want to go back to the Inspect screen again to get the XPATHs.
Later, he writes:
driver.find_element_by_xpath('//*[@id="guest_last_name"]')
The code should look something like:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/Users/name/Desktop/Selenium/chromedriver')
url = 'https://healthscreening.schools.nyc/?type=G'
driver.get(url)
last_name = driver.find_element_by_xpath('//*[@id="guest_last_name"]')
email = driver.find_element_by_xpath('//*[@id="guest_email"]')
button = driver.find_element_by_xpath('//*[@id="btnDailyScreeningSubmit"]/button')
# later you can click on these elements, pass characters to it with send_keys etc.
One suggestion that would really help while watching tutorials. Don't try to type along as the instructor is typing.
Watch it completely and try to understand the concept. Then do it yourself without watching. You can of course take a peek if you're stuck but the point is to type out the code from what you remember.
Then you could write your own code, automate something on your own. You'd encounter errors, you'll fix it and you'll learn even more.
You're doing great! Good luck on your journey.