Steve Staple
Steve Staple

Reputation: 3279

How can I find this element with Selenium / Java?

I am trying to find & click an element in a Test case built using Selenium & Java. In Firefox, the element has these properties:

OuterHTML: <text transform="translate(69,0)" text-anchor="middle" fill="#fff" style="font-family: Times New Roman; font-size: 32px; font-style: italic; font-weight: bold;" dominant-baseline="central">?</text>

InnerHTML: ?

CSS Selector: g.node:nth-child(4) > g:nth-child(1) > g:nth-child(4) > text:nth-child(2)

CSS path: html body.firefox div.gwt-PopupPanel.map-overlay-popup.fade-in div.popupContent div div.form-row.margin-0.ps.ps--theme_default div#layer-builder-svg-container.padding-50.padding-bottom-100 svg g.node g g text

 XPath: /html/body/div[8]/div/div/div/div[1]/svg/g[3]/g/g[3]/text

I have tried using the following:

driver.findElements(By.xpath("//text"));

driver.findElements(By.xpath("/html/body/div/div/div/div/div/svg/g/g/g/text"));

driver.findElements(By.xpath("/html/body/div[8]/div/div/div/div[1]/svg/g[3]/g/g[3]/text"));

All without success. Can anyone suggest a way I could find this element?

This was my final solution:

Common.myPrint(thisClass + " clickQuestionMark...");

    String textToFind="translate(69,0)";

    List<WebElement> elements = driver.findElements(By.xpath("//*[name()=\"text\"]"));

    Common.myPrint(thisClass + " elements count: " + elements.size());
    for (WebElement element : elements) {
        // select an element

            String text = Common.retryingGetAttributes(element, driver);
            if (text != null) {
                text = text.trim();
                Common.myPrint(thisClass + " text: " + text);
                if (text.contains(textToFind)) {
                    Common.myPrint(thisClass + " element found.");
                    Actions actions = new Actions(driver);
                    Common.myPrint(thisClass + " click on element.");
                    actions.moveToElement(element).click().build().perform();
                    Common.myPrint(thisClass + " click performed on element. " );
                    return true;
                }
            }

    }
    return false;

Upvotes: 0

Views: 99

Answers (2)

JeffC
JeffC

Reputation: 25542

You could just as easily use a CSS selector for this. I prefer CSS selectors over XPath whenever possible.

driver.findElements(By.cssSelector("text"));

Upvotes: 1

Steve Staple
Steve Staple

Reputation: 3279

The correct (working) answer came from @Andersson: Try XPath //*[name()="text"] – Andersson 1 hour ago

Upvotes: 1

Related Questions