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?
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.
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.
The syntax for locating elements through XPath- Multiple Attribute can be written as: //<HTML tag>[@attribute_name1='attribute_value1'][@attribute_name2='attribute_value2]
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With