如何在一个元素的两个条件下使用 WebDriverWait (Python, Selenium)
How to use WebDriverWait with two conditions for one element (Python, Selenium)
使用 selenium 和 python 创建一些 GUI 测试。我正在测试以确保按钮存在并且在单击它之前是蓝色的("active")...但有时颜色是十六进制,有时是 rgb,所以我需要检查我的网站中的任何一个driver等等。
这是我拥有的:
xpath = str(locator) + "[contains(@style, 'background: rgb(223, 239, 252)')]"
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
现在这可行了,但我想添加 OR 'background: #2E6E9E' 作为从等待继续的条件。
我使用 this post 了解如何在 css 样式上设置等待条件。
此外,将传递给定位器的示例是
"//button[@id='button1']"
所以 xpath 将是
"//button[@id='button1'][contains(@style, 'background: rgb(223, 239, 252)')]"
您可以通过多种方式完成此操作。您可以沿着您现在正在查看的路径前进,并使用带有 CSS 选择器或 XPath 的 OR。这些都有据可查。
另一种选择是创建自定义等待。 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.
中记录了这方面的示例
使用 selenium 和 python 创建一些 GUI 测试。我正在测试以确保按钮存在并且在单击它之前是蓝色的("active")...但有时颜色是十六进制,有时是 rgb,所以我需要检查我的网站中的任何一个driver等等。
这是我拥有的:
xpath = str(locator) + "[contains(@style, 'background: rgb(223, 239, 252)')]"
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, xpath)))
现在这可行了,但我想添加 OR 'background: #2E6E9E' 作为从等待继续的条件。
我使用 this post 了解如何在 css 样式上设置等待条件。
此外,将传递给定位器的示例是
"//button[@id='button1']"
所以 xpath 将是
"//button[@id='button1'][contains(@style, 'background: rgb(223, 239, 252)')]"
您可以通过多种方式完成此操作。您可以沿着您现在正在查看的路径前进,并使用带有 CSS 选择器或 XPath 的 OR。这些都有据可查。
另一种选择是创建自定义等待。 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.