hod
hod

Reputation: 761

Find element by Xpath using background color

I'm trying to grab an element (some date in a calendar) based on its background color. As you can see in this short example Sunday doesn't have a background color, but Monday does:

<td class="rcWeekend" title="Sunday, December 17, 2017"><a href="#">17</a></td><td title="Monday, December 18, 2017" style="background-color:#3C9770;">

I'm using Chrome to get an Xpath and I'm getting this:

//*[@id="ctl00_cphBody_rdcAvailableDates_Top"]/tbody/tr[4]/td[4]

I'm trying to make my path a little more robust and instead of selecting specific row and column I would just like to be able to get the very first available button with a background-color property. So I tried this:

browser.find_element_by_xpath(//*[contains(@style, 'background-color:#3C9770;')])

and this:

browser.find_element_by_xpath(//*[@id="ctl00_cphBody_rdcAvailableDates_Top"]/tbody/*[contains(@style, 'background-color:#3C9770;')])

And some other variations of that xpath... But I'm always getting an error. Can someone please advise how can I write this path correctly? Thank you!

Screenshot of the error: enter image description here

Upvotes: 0

Views: 2644

Answers (1)

iamsankalp89
iamsankalp89

Reputation: 4739

use quotes in xpath values

browser.find_element_by_xpath("//*[contains(@style, 'background-color:#3C9770;')]")

Upvotes: 3

Related Questions