Search code examples
pythoncssseleniumconditional-statementswait

How to use WebDriverWait with two conditions for one element (Python, Selenium)


Using selenium and python to create some GUI tests. I'm testing to make sure a button is present and blue ("active") before clicking on it...but sometimes the color is hex and sometimes it's rgb, so I need to check either one of those in my web driver wait.

Here's what I have:

xpath = str(locator) + "[contains(@style, 'background: rgb(223, 239, 252)')]"

WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))

Now this works, but I want to add OR 'background: #2E6E9E' as a condition to move on from the wait.

I used this post to find out how to condition a wait on a css style.

Also, an example of what would be passed into locator would be

"//button[@id='button1']"

So xpath would be

"//button[@id='button1'][contains(@style, 'background: rgb(223, 239, 252)')]"

Solution

  • There are a few ways you can accomplish this. You can go down the path you are looking at now and use an OR with either a CSS Selector or an XPath. These are well documented.

    Another option is create a custom wait. An example of this is documented in this answer. You could take advantage of element.value_of_css_property("background") which returns a string in an rgb format. Even if the format is specified in hex, it will return rgb so you won't need an OR in this case. You can also use the Selenium color support module documented here.