質問

I am using FluentAutomation library that wraps Selenium Web driver calls so the site can be navigated in a more behavioral manner.

As long as elements have id or other distinct properties, it works straightforward:

    I.Open(Site.BaseUrl);
    I.WaitUntil(() => I.Expect.Exists("#name-search"));
    I.Click("#applicationHost a");

But I need the driver to click on the buttons identified with the following XPath expressions:

//div[@id='questions']/div/div/div[4]/label/span[2]
//div[@id='questions']/div/div/div[2]/label/span[2]

etc. At least this is the XPath returned by Selenium IDE recorder. But I don't seem to be able to find right way of referencing these buttons. As you see, the only difference is the index of one of the divs (div[4], div[2]). Is there a common convention to refer to elements with such XPath?

UPDATE: Here's HTML extract from page inspector.

<div class="small-12 large-10 columns large-centered" data-bind="foreach: currentQuestion.alternatives">
            <div class="valg">
                <label data-bind="attr: { for: 'radio-' + $index() }" for="radio-0">
                    <input type="radio" name="radio-question-40" data-bind="attr: { for: 'radio-' + $index() }, value: value" class="hidden" for="radio-0" value="1">
                    <span class="enighet" data-bind="text: ($index() + 1)">1</span>
                    <span class="custom radio" data-bind="click: $parent.pickAnswer, css: { checked: $data.selected }"></span>
                    <span class="enighet" data-bind="text: text">Text for option 1</span>
                </label>
            </div>

            <div class="valg">
                <label data-bind="attr: { for: 'radio-' + $index() }" for="radio-1">
                    <input type="radio" name="radio-question-40" data-bind="attr: { for: 'radio-' + $index() }, value: value" class="hidden" for="radio-1" value="2">
                    <span class="enighet" data-bind="text: ($index() + 1)">2</span>
                    <span class="custom radio" data-bind="click: $parent.pickAnswer, css: { checked: $data.selected }"></span>
                    <span class="enighet" data-bind="text: text">Text for option 2</span>
                </label>
            </div>

        </div>
役に立ちましたか?

解決

So what i can suggest with XPATH is :

btn 1 : "//div[@class='valg'][position() = 0]/label/span[2]"

btn 2 : "//div[@class='valg'][position() = 1]/label/span[2]"

or in CSS selector

btn 1 : "div.valg:nth-child(1) label span.enighet"

btn 2 : "div.valg:nth-child(2) label span.enighet"

But i see that actually you have more div than what you've show. Tell me what's up.

他のヒント

Have the same problem, and i used the Selenium IDE for identificate the XPATH for each elemt, then use it like this:

driver.findElement(By.xpath("//div[@id='thalamusboapp-218829419']/div/div[2]/div[3]/div/div/div[2]/div/span/span") );

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top