ahmedaao
ahmedaao

Reputation: 397

Find element by XPath in Python

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 odds for 1xbet is 1.5, and I copy the XPath expression for this odds 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 an 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]

Upvotes: 0

Views: 4062

Answers (3)

Muhammad Hassan
Muhammad Hassan

Reputation: 14391

Try this XPath expression.

//div[@id="odds-data-table"]//td//a[contains(text(), "1xBet")]//ancestor::tr//td[2]

It's better to write your own XPath expression rather than copying from the browser as the browser gives a very tightly constructed XPath expression based on the element position relative to the nearest element with id or from your root. If any change occurs at any place, your XPath expression will not work.

Upvotes: 1

Pratik
Pratik

Reputation: 357

Try this XPath expression:

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

Upvotes: 2

Matthew Gaiser
Matthew Gaiser

Reputation: 4763

Your XPath expression is not correct.

Your XPath expression 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 odds 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()

Upvotes: 1

Related Questions