user12875715
user12875715

Reputation:

How to find element by style?

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

Upvotes: 0

Views: 1236

Answers (2)

JeffC
JeffC

Reputation: 25714

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']"));

Upvotes: 2

Atul
Atul

Reputation: 925

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='']

Upvotes: 0

Related Questions