pythonseleniumwebdriverinspectionfindelement

find_element_by_xpath() gives me Syntax Error by only copy a pasting Xpath from Safari


This is the website I want to take data: https://www.wunderground.com/history/daily/gb/london/EGLC/date/2017-2-10. I want to take the Day Average Temp which is 2.7 in this case. When I inspect the element in safari I can copy the row as a HTML,Xpath or Attribute. Since selenium can find elements by Xpath, I choose this method. However, when I copy and paste it in

driver.find_element_by_xpath(//*[@id="inner-content"]/div[2]/div/div[3]/div/div/lib-city-history-summary/div/div[2]/table/tbody[1]/tr[3]/td[1]) 

I get Syntax Error

A picture of the inspection.

If there is another way to select the element with out using an Xpath and that it works I could also use it.Thank you.


Solution

  • The xpath needs to be in quotes, which means you need ot either escape your double quotes:

    driver.find_element_by_xpath("//*[@id=\"inner-content\"]/div[2]/div/div[3]/div/div/lib-city-history-summary/div/div[2]/table/tbody[1]/tr[3]/td[1]") 
    

    Or change them to single quotes:

    driver.find_element_by_xpath("//*[@id='inner-content']/div[2]/div/div[3]/div/div/lib-city-history-summary/div/div[2]/table/tbody[1]/tr[3]/td[1]") 
    

    or, to more easily use your copy paste, surround your xpath in single quotes:

    driver.find_element_by_xpath('//*[@id="inner-content"]/div[2]/div/div[3]/div/div/lib-city-history-summary/div/div[2]/table/tbody[1]/tr[3]/td[1]')