4

I am currently developing automated UI tests with Appium for a website. I run my tests with many devices on testobject and there are some problems I try to solve.

My sample code is this:

WebElement lexiconCollapsible = mDriver.findElement(By.xpath("//*[@id='1014']/a"));
assertNotNull(lexiconCollapsible);
ScrollHelper.scrollToElement(mDriver,lexiconCollapsible);
Thread.sleep(1000);
lexiconCollapsible.click();

This is working for many devices but not for all of them. On some I get the following error code:

org.openqa.selenium.InvalidSelectorException: Argument was an invalid selector (e.g. XPath/CSS). (WARNING: The server did not provide any stacktrace information)

The exception is thrown at the position where I want to click the element, so the object is not null.

So my question is: Has anybody found a solution to check if the device is capable of finding the object? Is there something like a isObjectFound method for this?

I tried with css selector, id, etc. too but the results are the same.

Louis
  • 146,715
  • 28
  • 274
  • 320
Horsty
  • 321
  • 6
  • 18
  • 1
    Have you tried a wait? Wait for the XPath element to be visible and then continue. If you haven't and don't know how... let me know and I can add some sample code. – JeffC Oct 23 '15 at 13:38
  • I have added a custom wait method and tested again: First with cssSelector, I am getting the same error as before. Secondly with xpath and the result is: It is working now for all four devices! Thank you so much for you answer. My new wait command: `Utility.implicitlyWaitForElementPresent(mDriver,By.xpath("//*[@id='1014']/a"));` This is the correct answer. Thanks for all the help! – Horsty Oct 23 '15 at 14:14
  • No problem. Please post your working code as an answer and accept it so the question gets marked as answered. Thanks! – JeffC Oct 23 '15 at 15:32
  • I will do this on Monday. – Horsty Oct 23 '15 at 15:45

2 Answers2

1

From Selenium Docs,

exception selenium.common.exceptions.InvalidSelectorException(msg=None, screen=None, stacktrace=None)[source]
Thrown when the selector which is used to find an element does not return a WebElement. Currently this only happens when the selector is an xpath expression and it is either syntactically invalid (i.e. it is not a xpath expression) or the expression does not select WebElements (e.g. “count(//input)”).

So it looks like your Selector is incorrect and since it only happens with XPATH, you should try css selector.

Try,

WebElement lexiconCollapsible = mDriver.findElement(By.cssSelector("#1014 a")).click();

Regarding your isObjectFound method, it looks like the WebElement is found when you did findElement, you are getting an exception only on click(). I suggest you switch to CSS selectors to avoid this exception.

LINGS
  • 3,560
  • 5
  • 34
  • 47
  • Unfortunately as I said in my original comment, I tried switching to cssSelector and it doesn't help much. I tried your expression and my: "#1014>a" and the result is even worse. With xpath, I get more positive results. – Horsty Oct 23 '15 at 12:07
  • @minhundje With CSS selector what exception are you getting? – LINGS Oct 23 '15 at 12:32
  • I am getting: `org.openqa.selenium.InvalidSelectorException: Argument was an invalid selector (e.g. XPath/CSS). (WARNING: The server did not provide any stacktrace information)` and the test fails on finding the element. – Horsty Oct 23 '15 at 12:39
  • @minhundje In your question you have mentioned tests fail on clicking the element. If your test fails on finding the element, then it means the element is not present. or you have to wait for the element to be present. Post your HTML code. – LINGS Oct 23 '15 at 14:10
0

Answer provided by JeffC. It was a problem with the timeout and wait time in between the functions.

Utility.implicitlyWaitForElementPresent(mDriver,By.xpath("//*[@id='1014']/a"));
WebElement lexiconCollapsible = mDriver.findElement(By.xpath("//* [@id='1014']/a"));
assertNotNull(lexiconCollapsible);
ScrollHelper.scrollToElement(mDriver,lexiconCollapsible);
Thread.sleep(1000);
lexiconCollapsible.click();

It's working far better with xpath.

Horsty
  • 321
  • 6
  • 18