Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xpath for a div specifying a class and just one style attribute

Tags:

xpath

I want to write an xpath to identify a div which has the class foo and the display: block. I wrote

div[@class="foo" and @style="*display: block*"]

but it doesn't work. Is it correct to use and? is it correct to use asterisks like in regex expressions?

like image 911
nix86 Avatar asked Jul 06 '17 08:07

nix86


People also ask

Can we use class attribute in XPath?

We can find an element using the attribute class name with Selenium webdriver using the locators - class name, css, or xpath. To identify the element with css, the expression should be tagname[class='value'] and the method to be used is By. cssSelector.

How do I select a Div using XPath?

You have to select the parents first, then use a [#] predicate tag, then select all first children. //div[@class='row spacing-none']/div[1]//a[1] is the XPath you need to select the first a tag link on the whole page.

How do I combine two attributes in XPath?

The syntax for locating elements through XPath- Multiple Attribute can be written as: //<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]

How do you select the second element with the same XPath?

For example if both text fields have //input[@id='something'] then you can edit the first field xpath as (//input[@id='something'])[1] and the second field's xpath as (//input[@id='something'])[2] in object repository.


1 Answers

your xpath is searching for @style="*display: block*", it means totally equal to value inside quotes. Use contains() instead, Fyi: there's also starts-with() method

//div[@class='foo'][contains(@style,'display: block')]

There's an issue with using such locator, as sometimes element is visible, though there's no style display:block. So you can use similar locator, but instead of containing block, searche for not-containing "none" (as in display:none), can try it by adding this to main locator [not(contains(@style, 'none'))]. Just remember about such option)

like image 54
Vitaliy Moskalyuk Avatar answered Nov 19 '22 12:11

Vitaliy Moskalyuk