0

Element:

<div class="rsl-MeetingHeader_RaceName " style="">Moe</div>

Attempt at code:

from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromedriver = r"C:\Users\\Downloads\Python\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(executable_path=r"C:\Users\\Downloads\Python\chromedriver_win32\chromedriver.exe",chrome_options=chromeOptions)
driver.get("https://www.bet365.com.au/#/AS/B2/")
track_button = driver.findElement(By.xpath("//div[@class='rsl-MeetingHeader_RaceName' and style='Moe']"
track_button.click()
track_button.click()
    ^
SyntaxError: invalid syntax

Keep getting a syntax error

2 Answers 2

2

You are missing the closing parens in this line

track_button = driver.findElement(By.xpath("//div[@class='rsl-MeetingHeader_RaceName' and style='Moe']"))

The next issue is going to be that the element is not located. Your XPath is looking for an element with style='Moe' but your element has style=""... the contained text is 'Moe'.

The class name in your element also contains a space at the end so you will need to use

@class='rsl-MeetingHeader_RaceName '
                                  ^

You can either look for the empty style like

track_button = driver.findElement(By.xpath("//div[@class='rsl-MeetingHeader_RaceName '][@style='']"))

or ignore style and look for contained text like

track_button = driver.findElement(By.xpath("//div[@class='rsl-MeetingHeader_RaceName '][text()='Moe']"))

If that still doesn't work, it's possible the page is still loading so you will need to add a WebDriverWait and wait until the element is clickable, then click it.

track_button = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='rsl-MeetingHeader_RaceName '][text()='Moe']"));
Sign up to request clarification or add additional context in comments.

2 Comments

'Message: no such element: Unable to locate element' for both options.
@hpollock Updated the locators and added a wait example.
0

Below are some of the suggestions:

If the style is blank i.e. "", then you can use the below XPath:

//div[@style='']

enter image description here

If the style is not blank, then you can use the below XPath:

//div[@style='height: 0px;'][@class='fixed-nav-element-offset']

enter image description here

As per the code/HTML shared by you. You can use the below XPath as well:

//div[@class='rsl-MeetingHeader_RaceName ' and contains(text(),'Moe')]

OR

 //div[@class='rsl-MeetingHeader_RaceName ' and contains(text(),'Moe')][@style='']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.