使用来自 Selenium Webdriver 和 Python 的“By”检查元素是否存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17411671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 08:07:37  来源:igfitidea点击:

Checking if element is present using 'By' from Selenium Webdriver and Python

pythonwebdriver

提问by Dave

I am attempting to validate that text is present on a page. Validating an element by IDis simple enough, buy trying to do it with text isn't working right. And, I can not locate the correct attribute for Byto validate text on a webpage.

我正在尝试验证页面上是否存在文本。通过ID验证元素很简单,尝试使用文本进行验证是行不通的。而且,我找不到By的正确属性来验证网页上的文本。

Example that works for ID using Byattribute

使用By属性适用于 ID 的示例

self.assertTrue(self.is_element_present(By.ID, "FOO"))

Example I am trying to use (doesn't work) for text using Byattribute

示例我尝试使用(不起作用)使用By属性的文本

self.assertTrue(self.is_element_present(By.TEXT, "BAR"))

I've tried these as well, with *error (below)

我也试过这些,有 * 错误(如下)

self.assertTrue(self.is_text_present("FOO"))

and

self.assertTrue(self.driver.is_text_present("FOO"))

*error: AttributeError: 'WebDriver' object has no attribute 'is_element_present'

*错误:AttributeError:“WebDriver”对象没有属性“is_element_present”

I have the same issue when trying to validate By.Imageas well.

我在尝试验证时也遇到了同样的问题By.Image

采纳答案by Yi Zeng

First of all, it's discouraged to do so, it's better to change your testing logic than finding text in page.

首先,不鼓励这样做,与其在页面中查找文本,不如更改测试逻辑。

Here's how you create you own is_text_presentmethod though, if you really want to use it:

is_text_present如果您真的想使用它,以下是您创建自己的方法的方法:

def is_text_present(self, text):
    try:
        body = self.driver.find_element_by_tag_name("body") # find body tag element
    except NoSuchElementException, e:
        return False
    return text in body.text # check if the text is in body's text

For images, the logic is you pass the locator into it. (I don't think is_element_presentexists in WebDriver API though, not sure how you got By.IDworking, let's assume it's working.)

对于图像,逻辑是您将定位器传递给它。(is_element_present不过,我认为WebDriver API 中不存在,不确定您是如何By.ID工作的,让我们假设它正在工作。)

self.assertTrue(self.is_element_present(By.ID, "the id of your image"))
# alternatively, there are others like CSS_SELECTOR, XPATH, etc.
# self.assertTrue(self.is_element_present(By.CSS_SELECTOR, "the css selector of your image"))

回答by jeanM

From what I have seen, is_element_present is generated by a Firefox extension (Selenium IDE) and looks like:

据我所知, is_element_present 是由 Firefox 扩展(Selenium IDE)生成的,看起来像:

def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException: return False
    return True

"By" is imported from selenium.webdriver.common:

“By”是从 selenium.webdriver.common 导入的:

from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

There are several "By" constants to address each API find_element_by_* so, for example:

有几个“By”常量来处理每个 API find_element_by_* 因此,例如:

 self.assertTrue(self.is_element_present(By.LINK_TEXT, "My link"))

verifies that a link exists and, if it doesn't, avoids an exception raised by selenium, thus allowing a proper unittest behaviour.

验证链接是否存在,如果不存在,则避免 selenium 引发的异常,从而允许正确的单元测试行为。

回答by user773093

I like wrapping the whole thing into a custom assertion

我喜欢把整个事情包装成一个自定义断言

from selenium.common.exceptions import NoSuchElementException

def assertElementIsPresentByXPath(self, xpath, msg=None):
    try:
        self.browser.find_element_by_xpath(xpath)
        self.assertTrue(True, msg)
    except NoSuchElementException:
        self.assertTrue(False, msg)

def test_element_10_should_exists(self):
    self.browser.get('url/to/test')
    self.assertElementIsPresentByXPath('//a[@id=el_10]')