python find element by xpath

I would like to extract some odds from the website :

https://www.oddsportal.com/soccer/europe/champions-league-2015-2016/real-madrid-manchester-city-dhJZn1pn/#1X2;2

For example the home odd for 1xbet which is 1.5, I copy the xpath for this odd and insert it into find_element_by_Xpath

Here is my little code :

> from selenium import webdriver from bs4 import BeautifulSoup
> 
> driver = webdriver.Firefox() url =
> 'https://www.oddsportal.com/soccer/europe/champions-league-2015-2016/real-madrid-manchester-city-dhJZn1pn/#1X2;2'
> driver.get(url) print(driver.title)
> 
> element =
> driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[1]/table/tbody/tr[2]/td[1]/div/a[2]")
> 
> print(element.text)
> 
> driver.close() 

Unfortunately the answer is a error :

hao@hao-ThinkPad-T420:~$ ./testodds.py Real Madrid - Manchester City Betting Odds, Soccer - Champions League 2015/2016 Traceback (most recent call last): File "./testodds.py", line 12, in element = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[1]/table/tbody/tr[2]/td[1]/div/a[2]") File "/home/hao/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath return self.find_element(by=By.XPATH, value=xpath) File "/home/hao/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element 'value': value})['value'] File "/home/hao/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/home/hao/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[1]/table/tbody/tr[2]/td[1]/div/a[2]

2 answers

  • answered 2020-01-21 20:18 Matthew Gaiser

    Your xpath is not correct.

    Your xpath is this: "/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[7]/div[1]/table/tbody/tr[2]/td[1]/div/a[2]"

    You want this: /html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[8]/div[1]/table/tbody/tr[1]/td[2]/div

    Here is the full working code. I successfully scraped the odd with it.

    from selenium import webdriver
    from bs4 import BeautifulSoup
    
    driver = webdriver.Firefox()
    url ='https://www.oddsportal.com/soccer/europe/champions-league-2015-2016/real-madrid-manchester-city-dhJZn1pn/#1X2;2'
    driver.get(url)
    print(driver.title)
    element = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div[6]/div[1]/div/div[1]/div[2]/div[1]/div[8]/div[1]/table/tbody/tr[1]/td[2]/div")
    print(element.text)
    driver.close()
    

  • answered 2020-01-21 22:19 Pratik

    Try this xpath:

    .//table[@class='table-main detail-odds sortable']/tbody/tr/td[2]/div