Trying to click link via selenium in java - java

Using selenium in java. We have a series of links that look approximately like:
<a href='javascript:newWin("/ABC")'>ABC</a>
We're trying to click that link. There's no id unfortunately. We've tried a few things along the lines of the following.
driver.findElement(By.xpath("(//[contains(#href, 'javascript:newWin')])")).click();
Which results in:
Unable to locate an element with the xpath expression
(//[contains(#href, 'javascript:newWin')]) because of the following
error: [object Error] (WARNING: The server did not provide any
stacktrace information)
We're trying to figure out how to XPath to that anchor, and click it. We can't get an ID on it (at least not yet).

Try this one
driver.findElement(By.xpath("//a[text()='ABC']").click();

I think you're XPATH is malformed, but you're on the right track. Try:
driver.findElement(By.xpath("//a[contains(#href, 'javascript:newWin')]")).click();

Related

Java Selenium - POM - html xpath - no such element : Unable to locate element

I'm getting this error while trying to find the web element for testing the facebook "create a page">"sign up" button under Page Object Model. I tried various options like by class name(CSS selector) and also by copying the system xpath but getting the same error message
org.openqa.selenium.NoSuchElementException: no such element : Unable to locate element
/driver.findElement(By.xpath("//*[#id='blueBarDOMInspector']/div/div[1]/div/div/span/a")).click();
driver.findElement(By.className("_42ft _4jy0 signup_btn _4jy4 _4jy2 selected _51sy"));
Looks like you class name generates randomly - you can check it by re-visiting same page from different browsers (or you can try incognito). If it's true you need another way to work with element. Some examples:
If this Sign Up button - use xPath and look for the text in that element
Think if your task can be resolved without using interaction with UI (or part of the task). Read about facebook API and if possible use this instead of UI. It will save you a lot of time and hassle down the road.

Unable to click on a "text" link in google search page

I am trying to click on News link on google search page the HTML structure looks like this
I tried following xpaths but none worked
//a/child::span[1][contains(.,'News')]
The following xpath resulted in invalid selector: The result of the xpath expression "//a/child::span/following-sibling::text()[contains(.,'News')]" is: [object Text]. It should be an element.
//a/child::span/following-sibling::text()[contains(.,'News')]
Thanks
//a[contains(.,'News')] might return this link, but may result in a list of more than one element that you'd need to handle and select the right element from.
You can use Selenium's SearchContext to specify a container element, or solve it using an xpath one-liner like: //div[#role='navigation']//a[contains(.,'News')] (Effectively searching for a link that contains 'News' somewhere in it's html-tree, somewhere inside a div that has a role attribute with value 'navigation').
You simply need
//a[contains(., "News")]
Note that "News" is not a part of span, but a, so your 1st XPath won't work

Selenium: find part of identifier

The next command does not work
wd.findElement(By.id("post_message_*"));
The goal is to find id's with post_message_3456346, post_message_01548 and so on. How to fix it?
Actulay there is only one id on page which starts with "post_message_".
Try to locate required element by XPath as below:
wd.findElement(By.xpath("//*[starts-with(#id, 'post_message_')]"));
or by CSS selector
wd.findElement(By.cssSelector("*[id^='post_message_']"));
Try using a css Selector
wd.findElement(By.cssSelector("[id^='post_message_']"));

Java Selenium. Find element by text for certain xpath

Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]

How to click on a specific link with Selenium?

I have an integration test. It uses Selenium, from Java. I would like to click on a specific link, which has no ID. All I know about it is that it is in a specific cell in a specific table. The table has an ID.
How can I click on that element using DefaultSelenium?
I tried
defaultselenium.click("th.tblHeader:nth-child(14) > a:nth-child(1)");
because Firefox said that it's the XPath of the element, and I also tried
defaultselenium.click("#tableId > thead:nth-child(1) > tr > th.tblHeader:nth-child(14) > a:nth-child(1)");
because I don't use XPath very often and I had no idea what I was doing, but I thought that having a unique ID in the equation may help.
Both of them give me "ERROR: Element ... not found" messages.
That's not XPath, it's CSS. Vanilla Firefox isn't helpful for extracting XPath from a page, but there are plugins available. Alternatively, Chrome will give you the XPath.

Categories

Resources