how to click in href link with selenium-java - java

i'm working with selenium-java to automate some tests (it's self learning). I'm stuck in click a hyperlink, but this href is pretty particular, because like this:
<a tabindex="-1" href="../../myWebPage.html"><span>My Web Page</span></a>
My java code is:
1.- driver.findElement(By.xpath("//a[#href='../../myWebPage.html']")).click();
2.- driver.findElement(By.xpath("//a[#href='https://RealHost/pag1/myWebPage.html']")).click();
The second option that i used is with the real link, but non of them is working.
Could you please help me?
P.S: I also used the option driver.findElement(By.LinkText("https://RealHost/pag1/myWebPage.html")).click(); but without success.
Thanks guys!

You should be able to locate the link element by first finding its parent element by looking through the DOM and getting the xpath for that.
Then use that parent element to find elements with a tag of "a"
WebElement parent = findElement(By.xpath("/*path to parent element here*/"));
parent.findElement(By.tagName("a")).click();
Note that the parent element may have multiple children of tagName "a" if that is the case use findElements() to get a collection of all the hyperlinks with that parent. Then search the collection for the one you want.
WebElement parent = findElements(By.xpath("/*path to parent element here*/"));
List<WebElement> elements = parent.findElements(By.tagName("a")).click();
//search the list for the correct link
Another thing you could try would be to locate the element by linkText.
findElement(By.linkText("/*The hyperlinks text*/")).click();
hope this helps!

Try either of the xpath.
driver.findElement(By.xpath("//a[contains(#href,'/myWebPage.html')]//span[text()='My Web Page']")).click();
OR
driver.findElement(By.xpath("//a[.//span[text()='My Web Page']]")).click();
OR css selector
driver.findElement(By.cssSelector("a[href*='/myWebPage.html']>span")).click();

this works for me:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("My Web Page"))).click();
i added a wait overe the element to be clickable and it's works
Thanks guys for your help.
Bye

this works fine for IE driver, but with Chrome is failing due to:
stale element reference: element is not attached to the web page
Help please!

Related

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

How to click a button where the class is the same for another options?

I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.

Why am I not able to click the web element using the following code, even though I can see that the controller is tracking the xpath?

click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);

How can I get text from element with Selenium webdriver and Java?

My code:
WebDriver driver = new SafariDriver();
driver.get("http://bet.hkjc.com/football/default.aspx");
WebElement matchs = driver.findElement(By.cssSelector("span.Head to Head"));
System.out.println(matchs);
driver.quit();
How can I crawl Manchester Utd and Celta Vigo?
WebElement matchs = driver.findElement(By.xpath("//a[#title='Head to Head']"));
System.out.println(matchs.getText());
Use firebug and firepath addons in firefox and inspect that element and get the xpath and put it here inside double quotes in this code :
System.out.println(driver.findElement(By.xpath("")).getText());
If you don't know how to use firebug and firepath refer this link
You can locate the element either by css selector or xpath selector
By using xpath
driver.findElement(By.xpath("//a[#title='Head to Head']"));
By using css Selector
driver.findElement(By.cssSelector("span > a[title='Head to Head']"));
OR Try somethings like this if not getting the match
driver.findElement(By.cssSelector("td.cteams.ttgR2>span>a[title='Head to Head']"));
Note : in your code you are trying like span.Head to Head in CSS selector . dot represents the class and according to your path you are locating span tag which have class name "Head to Head" which doesn't exist in your dom as this is the title of anchor tag.
Went through the Firebug and Firepath plugins of Firefox initially to get the Xpath or css path
Explore some blogs to get clear understanding, you will be able to create by yourself
Refer This link for the same
I assume all the above answers doesn't work for you and am providing another answer.
I can see both the texts are under "a" tag. So the idea is to navigate to the element and use getText() - which returns the visible text.
String word = driver.findElement(By.xpath("//span/a")).getText();
System.out.println(word);
Hope this works for you.
In all of my tests I'm using the getAttribute like this to get text and it is working fine for me on all drivers :
assertEquals(strCity, txtCity.getAttribute("value"));

selenium.ElementNotVisibleException: Element is not currently visible JAVA

I am working on selenium, while running Java code I tried to CLICK a menu from the web page but encounter error of selenium.ElementNotVisibleException: Element is not currently visible Kindly advise on this matters . Thanks you
HTML code for text field :
<li onclick="goin('pages/AbcProxy/proxyGroupList.do')>
TESTABC
JAVA code:
WebdriverWait wait = new WebDriverWait(driver,50);
wait until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
presenceOfElementLocated checks if the element exists in the DOM. To check if the element is visible use visibilityOfElementLocated
WebdriverWait wait = new WebDriverWait(driver,50);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
element.click();
It might be that your xpath is to a different a element that is invisible (css display none or such).
It would be better to use some id first (maybe for the parent ul?) and then a relative xpath from there, full xpaths from root are not recommended as it makes the test very brittle

Categories

Resources