2

I'm working with an old portal and I have to use IE. There are some things that it doesn't find, cause it's part of a <td> menu, I tried to find it by XPath, but doesn't help.

I found the form is being rendered by a JavaScript function. And I'd like to click on them just to execute it, but how can I locate the page elements using selenium WebDriver??

For example: if I had this code

<div class="logout-link ng-scope" 
     ng-click="login(&quot;github&quot;)" 
     ng-show="!me" ng-controller="MenuCtrl">login</div>

How can I execute the ng-click part with the Selenium WebDriver?

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Nancy Cruz
  • 115
  • 1
  • 10
  • What do you mean by `I tried to find it by XPath, but doesn't help.`..is there any exception or anything else..share it as well – Saurabh Gaur Jul 21 '16 at 17:29
  • Well, because I don't have the real code of the web. It's in another pc where I cannot extract any code, so I decided to put another kind of example. There was a toolbar and the xpath found each tool but I just couldn't click at one because all of the had the same atributes but the javascript. So I needed to execute one javascript in specific. That's why. – Nancy Cruz Jul 21 '16 at 20:26
  • I like how accepted answer has nothing to do with tittle. https://stackoverflow.com/questions/11688694/selenium-error-when-using-javascript-or-getting-elements – George Aug 17 '17 at 09:30

3 Answers3

1
  1. Make sure you're trying to find element in the same frame it is located. Answer example: How to switch between frames in Selenium WebDriver using Java
  2. Try to wait for element to appear and be available: http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
  3. Hopefully you know how to find elements via JS (document.getElementsByClassName('logout-link ng-scope')) and here is answer on hot to use JS in C#: Execute JavaScript using Selenium WebDriver in C# - only difference is that you don't need to return anything - you only need to '.click()'
Community
  • 1
  • 1
Mikhail
  • 665
  • 4
  • 16
  • Thank you. It doesn't works with frame, because the web doesn't have any frame. I actually used the wait, and it works great. This is not the real code, in the real code doesn't have any id or classname that can be identified. – Nancy Cruz Jul 21 '16 at 21:01
0

Clicking on the web element you create executes the associated function. Look via CSS Selector against the ng-click:

IWebElement elem = driver.FindElement(By.CssSelector("div[ng-click=login(&quot;github&quot;)]"));
elem.click();

You could also build an action to move to the element and then click on it:

IWebElement elem = driver.FindElement(By.CssSelector("div[ng-click=login(&quot;github&quot;)]"));
Actions action = new Actions(driver);
action.MoveToElement(elem).Click().Build().Perform();
rg702
  • 92
  • 1
  • 5
0

Why do you want to execute Javascript to locate an element??? Try using WebDriverWait to wait until element visible and clickable as below :-

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

var Login = wait.Until(ExpectedConditions.ElementToBeClickable(By.Xpath("//div[text() = 'login']")));
 Login.Click();

Note :- make sure before try this element is not inside any frame

Hope it helps.....:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    You were right, this actually worked for me, thank you so much! – Nancy Cruz Jul 21 '16 at 20:21
  • @NancyCruz you welcome. glad to help..:)..I'm suggesting next time when you will ask question, provide atleast some enough information that which have you tried and what was the result, if exception will share it as well that's why experienced person could give you a correct solution ASAP without confusion... anyway Happy learning...:) – Saurabh Gaur Jul 21 '16 at 22:27