How to locate this <a> element with Selenium? - java

There is a button “Apply Now” on http://yearup.org.
Apply Now
So far I tried:
//*[#class='button']") (but there are 11 occurrences. Using [0] doesn't help
//*[#class='button']//*[text()='Apply Now']
"html/body/div/section[2]/header/section/ul/li/ul/div[2]/a"
.findElement(By.linkText("Apply Now"));
None of them worked for me.
Following error is obtained:
org.openqa.selenium.ElementNotInteractableException: Cannot click on element

What makes it a bit hard, is a duplicate of same tag in same page.
There are two solutions:
//div[#class='large-9 columns large-centered center-absolute']/a[#class='button' and contains(text(),'Apply Now')]
or
(//a[contains(text(),'Apply Now')])[2]

This will work:
driver.findElement(By.xpath(".//*[#href='http://www.yearup.org/seize-opportunity/']")).click();
If you are only trying to locate it and not click the element, then use this:
driver.findElement(By.xpath(".//*[#href='http://www.yearup.org/seize-opportunity/']"));
And further, if you are trying to travel to that page and you don't need to actually click on the element to travel there, just do:
driver.get("http://www.yearup.org/seize-opportunity/");

I know in the HTML it shows up as "Apply Now" but if you use
driver.findElement(By.linkText("APPLY NOW")).click();
it works. I just tested it.

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.

Having trouble clicking a link with selenium webdriver/java

I'm trying to click the link below:
<td id="mainleftlinkzoneover" class="mainleftlinks" width="151" title="Student Medicaid Eligibility">
Here's the code. It works with other links like this but not this one.
WebElement myElement = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("td[title='Student Medicaid Eligibility']")));
myElement.click();
Anyone have any ideas what I'm doing wrong?
you can do it by using the below code:
By.id("mainleftlinkzoneover") //here only id is used
or
By.cssSelector("#mainleftlinkzoneover[title='Student Medicaid Eligibility']") // this is another way to locate element
use the any one instead of your By.cssSelector.
Make sure there are no frame.
Is the link available for others to check? I mean can I test it if I want? you can give the url if possible.
here are my 2 cents:
I am thinking because of the spaces in your title, you might not be able to click on it.
I would try using contains
something like By.xpath("//td[contains(#title,'Eligibility')]") you can put either student or medicaid if they are unique. See if that helps.

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')]

selenium: How to select a button to click , if the same button already exists on the same page with same name,value,id?

actually this button open on the popup, but exist in the same page .
![enter image description here][1]
I tried to read the `.... ' with the below code
driver.findElement(By.tagName("td")).findElement(By.id("leadCaptureList_leadCaptureList_assignCampaign")).click();
but its showing error like "Unable to locate element: {"method":"id","selector":"leadCaptureList_leadCaptureList_assignCampaign"}"
If the other button is not in a td, then you can use following code.
driver.findElement(By.xpath("//td/input[#id='leadCaptureList_leadCaptureList_assignCampaign']")).click();
On the other hand, if both have same xpath as well, then you can use index with xpath as follows:
driver.findElement(By.xpath("(//td/input[#id='leadCaptureList_leadCaptureList_assignCampaign'])[1]")).click(); //you will have to give the correct index, I am giving [1] as example
If this doesn't work as well (which I doubt), then I'll need the 2nd button's html as well, to find out the difference between two button.
Just like Husam said, add parent to the selector, such as the div popped up.
//assume popup div id is popup
$("#popup #button").click() may be the one you want.
Hopes this helps

Categories

Resources