0

An element can have two views of XML:

1.

<div class="GRUPP_FLG">
    <div role="radiogroup">
        <div class="radioButton">
            <div role="radio" aria-label="Yes">
                <input type="radio">
                <span>Yes</span>
            </div>
        </div>
        <div class="radioButton">
            <div role="radio" aria-label="No">
                <input type="radio">
                <span>No</span>
            </div>
        </div>
    </div>
</div>

2.

<div class="GRUPP_FLG">
    <div>
        <span title="No" class="readonlyField">
            No
        </span>
    </div>
</div>

In the first case, I need to click on radiobutton.

WebElement radioButton = elementWithRadiogroup.findElement(By.xpath(".//*[@aria-label='No']"));
radioButton.click();

In the second case, I need check that span has a class readonlyField

if(!elementWithSpan.getAttribute("class").contains("readonlyField"))
    throw new AutotestError("Error");

if use

//div[contains(@class, 'GRUPP_FLG')](//*[@role='radiogroup'] | //span[contains(@class, 'readonlyField')])

then happens error:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the XPath expression 
because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document'

How could I get XPath?

1
  • Remove the round bracket s from the xpath. Commented Dec 12, 2019 at 12:41

2 Answers 2

1

Try the below Xpath this should work for you.

//div[contains(@class, 'GRUPP_FLG')]//*[self::*[@role='radiogroup'] | self::span[contains(@class, 'readonlyField')]]

EDIT:

//div[contains(@class, 'GRUPP_FLG')]//*[self::input | self::span[contains(@class, 'readonlyField')]]

Snapshot:

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

Why for //div[contains(@class, 'GRUPP_FLG')]//*[self::input | self::span[contains(@class, 'readonlyField')] may happens error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[contains(@class, 'GRUPP_FLG')]//*[self::input | self::span[contains(@class, 'readonlyField')]' is not a valid XPath expression. ?
@NeverSleeps : You have missed the end bracket. I have edited that.Hope this will work for you.
thanks for your help alas, the bracket did not help."//*[self::input | self::span]" works, but //*[self::input | self::span[contains(@class, 'readonlyField')]] fails
@NeverSleeps : Strange.When I am using your example I can see three elements are selected.Check the snapshot.
I swapped elements and it worked. //*[self::span[contains(@class, 'cpbReadonlyField')] | self::input] Thanks!
0

To validate if the <span> has the class readonlyField you can use:

try {
  driver.findElement(By.xpath("//div[@class='GRUPP_FLG']/div/span[@title='No' and @class='readonlyField']"))
  System.out.println("span has the class readonlyField");
}
catch(NoSuchElementException e) {
  System.out.println("span doesn't have the class readonlyField");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.