1

I have a link for which I can grab the inner text, but I cannot click it

driver.switchTo().activeElement();
String sTemp = selenium.getText("TabBar:LogoutTabBarLink");

the above works fine and

sTemp = "Log Out";
selenium.click("TabBar:LogoutTabBarLink");

the above fails for: Element is no longer valid

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
user2755572
  • 11
  • 1
  • 3
  • 1
    I am confused about your usage of Selenium RC and WebDriver together. Do you use an instance of the [`WebDriver` interface](http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.html), or an instance of [Selenium](http://selenium.googlecode.com/svn/trunk/docs/api/java/com/thoughtworks/selenium/Selenium.html)? You initial tagging and your first line of code says the former, but `getText()` and `click()` methods are from the latter. – Petr Janeček Sep 06 '13 at 20:04
  • I am really not using RC. I have only been coding selenium for a couple of weeks, so I am easily confused. I am straddling two worlds by first setting a driver, then setting selenium thusly: public void setDriver(WebDriver driver) { this.driver = new InternetExplorerDriver(); } public void setSelenium(Selenium selenium, String sURL) { this.selenium = new WebDriverBackedSelenium(driver, sURL); } – user2755572 Sep 06 '13 at 20:13
  • 1
    If you can, stick only to WebDriver and forget the Selenium interface. WebDriver is newer, has better API, is still developed etc. Selenium is oficially deprecated, has not been developed in 2 years, has it's shortcomings etc. Do you have to use Selenium? If not, try to rewrite your code to WebDriver equivalent and see if the problem goes away or not. – Petr Janeček Sep 06 '13 at 20:17
  • Thanks for the advice, I will drop the Selenium interface. Google makes it too easy to find random solutions! Another kind soul (below) is telling me to use xpath, so I will run with that. – user2755572 Sep 06 '13 at 20:25

2 Answers2

2

You are really want to use Selenium 2 (aka Web Driver), it's much more faster and reliable. So forget about Selenium 1 unless you have a really good reason to use it. Your code should look like this

WebDriver driver = new FirefoxDriver(); //or any other, like ChromeDriver or OperaDriver
WebElement element = driver.findElement(By.id("idOfYourElement");
element.click();
String text = element.getText();

Also see the 5 minute getting started guide

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • I figured it out, finally. Rookie mistake. This web application follows an external link, does some stuff and then returns control. – user2755572 Sep 07 '13 at 13:48
  • 1
    I am glad you solved it. Maybe one more thing, you should accept the answer you consider as most helpful:-) – Petr Mensik Sep 07 '13 at 17:31
0

Try just using the WebDriver, and getting the element by id or by xPath (if there's no id).

You can write your code like this for xpath:

driver.switchTo().activeElement();
WebElement element = driver.findElement(By.xpath(/*Insert xpath Here */);
element.click();

You can use Firebug in Firefox or Chrome's debugger in Chrome to find the xpath of the element.

If it has an id, you can find the element by id similarly (it's faster and thus more preferable):

WebElement element = driver.getElement(By.id(/*Insert id Here */);

See my answer to this question for more useful info on finding by id and/or xpath.

Community
  • 1
  • 1
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • Thanks. Up till now, I have been finding elements by HTML id, (thanks to Mozilla FireBug) so now I will go to xpath. – user2755572 Sep 06 '13 at 20:28
  • 1
    With the exception of generated (and therefore changing) ids, it is always better to use id than any kind of XPath expression. – Petr Janeček Sep 06 '13 at 20:29
  • If it has an id, you should use the id, as it's faster. Updating my answer to mention that. I only suggested xpath in case the element didn't have an id and you didn't have ability to change it. – James Dunn Sep 06 '13 at 20:29
  • As a rule of thumb, selecting by ID is better than class, which is better than CSS which is about equal with XPath. Tag name and link text have their own little worlds, so I left them out. – Nathan Merrill Sep 06 '13 at 22:12
  • I finally figured it out. Rookie mistake. My app follows an external link, does some stuff, and then returns control. Here is what I do after I return to the main app:pca.driver.switchTo().activeElement(); pca.driver.switchTo().frame(pca.driver.findElement(By.id("top_frame"))); at the start of my program I do the switchTo().frame immediately after instantiating the IE8 browser. I just had to do it again. THANKS FOR THE HELP! – user2755572 Sep 07 '13 at 13:55
  • why this line `driver.switchTo().activeElement();` is needed ? – Arup Rakshit Sep 09 '13 at 09:02