SELENIUM: how to check if a particular text is present in a particular attribute of HTML tag

拜拜、爱过 提交于 2020-01-06 14:18:16

问题


I have a HTML tags:

<a href="rid=7059"> abc </a>
<a href="pid=8059"> fgh </a>
<a href="cid=9059"> cxq </a>

HOw do I check if a particular tag exists with the href attribute containing rid (First tag in the above example)

get_attribute() gets the value of a specified attribute but how do i check when in a page any such tag exists whose attribute value is rid ?

Thanks for the help.


回答1:


You didn't mention the language you are using, so from personal preference I'm assuming Python. Other languages shouldn't really differ, though.

The is_element_present function can be used for this. From the Selenium source:

def is_element_present(self,locator):
    """
    Verifies that the specified element is somewhere on the page.

    'locator' is an element locator
    """
    return self.get_boolean("isElementPresent", [locator,]

For the locator you could use something along the lines of "css=a[href*='rid']".




回答2:


When locating the element, use XPath expression like //a[contains(@href,'rid')], so isElementPresent(//a[contains(@href,'rid')]) should do.



来源:https://stackoverflow.com/questions/7804722/selenium-how-to-check-if-a-particular-text-is-present-in-a-particular-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!