如何为Python代码中driver.get()的link_of_item添加单引号?
driver.get() Call in Your Python Code Hey there! Let me clear up a common misconception first—if you change driver.get(link_of_item) to driver.get('link_of_item'), you’re not using the URL the user input anymore. Instead, you’re telling the driver to load the literal string 'link_of_item' as a URL, which will throw an error because that’s not a valid web address.
What You Actually Need (And Why)
The variable link_of_item already stores the URL as a string (since input() returns a string by default). So the original driver.get(link_of_item) is the correct way to pass the user-provided URL to the driver.
If your goal was to add single quotes around the URL value (for display, logging, or debugging purposes), you can do that when you output the URL, not when passing it to driver.get(). Here’s the adjusted code:
link_of_item = input('Please past here the link of item from newegg:\n') # Correct way to load the user's URL driver.get(link_of_item) # Optional: Display the URL with single quotes for clarity print(f'Loaded URL: \'{link_of_item}\'') print('name of item: ', driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div/div/div[2]/div[1]/div[5]/h1').text) print('Price of item now is: ', driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[1]/div/div/div[1]/div[1]/div[1]/div[3]/div[1]/div[2]/strong').text) price = input("write the price you want get: ")
Key Takeaway
- Variables don’t need quotes when you’re referring to their values. Quotes turn text into a literal string, which breaks the connection to the user’s input.
- Keep
driver.get(link_of_item)to load the actual URL the user pasted.
内容的提问来源于stack exchange,提问作者chen sliktar

