简体   繁体   中英

Pass a complicated xpath through Java Script Executor in Selenium Webdriver

I have the used the following xpath to find element on a grid.

By.xpath("//div[contains(text(),'" +EnteredCompetitionName+ "')]/preceding-   sibling::div[contains(concat(' ', @class, ' '), ' slick-cell l0 r0 ')]/a/img"

The problem is the grid is getting too large and the element I am trying to find cannot be found simply using find element by xpath. Hence I tried using the javascript executor in selenium webdriver.

WebElement selectedCompGlass = (WebElement) js.executeScript("return document.evaluate('//div[contains(text(),' +EnteredCompetitionName+ ')]/preceding-sibling::div[contains(concat(\' \', @class, \' \'), \' slick-cell l0 r0 \')]/a/img' ,document, null, XPathResult.ANY_TYPE, null ).singleNodeValue;");
js.executeScript("arguments[0].click();", selectedCompGlass);

However, I am still getting the following error:

missing ) after argument list Command duration or timeout: 8 milliseconds Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'

If scrolling works in your case, then u can use javascript to scrollIntoView untill it finds the element and then do your actions.

It will scroll to the element,

WebElement element = driver.findElement(By.id("id_of_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

You can filter the FindElement result using other FindElement, or some Selenium method to select your result. Did you tried it?

As example, in Selenium C# I write something like it yesterday:

var element = browser.FindElements(By.TagName("input"), 5000)
    .FirstOrDefault(x => x.GetAttribute("data-id-selenium") == "entrar");

And other example

Browser.FindElements(By.CssSelector("li[class='item-listagem']"), 5000)
    .First(x => x.Text.Contains(titulo))
    .FindElement(By.CssSelector("a[href*='AprovarAcaoLiderado']"))
    .Click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM