2

I m trying to find the element "import" using Selenium Webdriver in C#. Have tried the following codes but nothing find it.

driver.FindElement(By.XPath("//*[@class='menu_bg']/ul/li[3]")).Click();
driver.FindElement(By.XPath("//*[@id='import']/a")).Click();
driver.FindElement(By.CssSelector("#import>a")).Click();
driver.FindElement(By.XPath("//*[@class='menu_bg']/ul/li[3]/a")).Click();
driver.FindElement(By.CssSelector("ul[@class='menu_bg']>li[value='3']")).Click();

Please help me out. Design page looks like below:

<body>
    <div class="header_bg"></div>
    <div class="menu_bg">
        <ul class="menu">
            <li id="retrieve"></li>
            <li id="scan" class="test"></li>
            <li id="import">
                <a target="main" href="import/import.aspx" onclick="clickme(this,'import')">Import</a>
            </li>
            <li id="admin"></li>
            <li id="help"></li>
            <li style="float: right;"></li>
        </ul>
    </div> 
</body>

All the time I got the error as below:

unable to find the element
2
  • Give a try for: driver.FindElement(By.XPath("//li[@id='import']/a")).Click(); Commented Jun 10, 2014 at 9:50
  • You can also try: driver.FindElement(By.id("import")).Click(); Commented Jun 10, 2014 at 9:51

3 Answers 3

3

XPath indexers are 1-based, as opposed to most other languages whereby they are 0-based.

This means you are actually targetting the 2nd li element, which has no anchor element.

So:

//*[@class='menu_bg']/ul/li[3]/a

However, this XPath query is not great and is too strict on position - thus although this newly fixed XPath above should work, I'd advise you to think of something else.

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

2 Comments

+1, for pointing out XPaths like this one should be avoided in the first place.
@Arran: Thanks for your suggestion. But still i got the below error - Unable to find element with xpath == //*[@class='menu_bg']/ul/li[3]/a
1

By reviewing this link(Thanks to @Arran), the above issue was fixed. 'switching' to the current IFrame directs Selenium to show any requests to that frame instead.

driver.SwitchTo().Frame() 

1 Comment

Unfortunately you didn't show us the page had any IFrames in it so we couldn't have possibly have known this would solve the issue. Nonetheless, glad you got it solved :)
0

You can do this by chaining Selenium 'FindElement' like so;

driver.FindElement(By.Id("import")).FindElement(By.TagName("a")); which will give you the child of the element with ID that has a tag of 'a'.

Another way you could do this is by casting your Driver to an IJavascriptExecutor and executing javascript directly in the browser using a JQuery selector. I find this better for more complex Selenium lookups;

((IJavascriptExecutor)Driver).ExecuteScript("$("a[target='main'][href='import/import.aspx'])").click();

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.