Having trouble clicking a link with selenium webdriver/java - 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.

Related

How to check if Selenium clicks on element successfully?

In my java and Selenium project, I have the below method to click on a specific element
public void click(String xPath) {
driver.findElement(By.xpath(xPath)).click();
}
Currently, i have an element that it does not react on my click! So, i need to make sure, if selenium really successfully click on this element.
Point: There is no error during the runtime. So, i think it can find the element. But, it is a surprise for me, that there is no effect of click!
More details:
I am trying to automate this page. You can find the complete code in this repository. You need to take a few steps till you reach to the point that i really have the problem. That's why i share the repository, since the page is not directly accessible (sorry for that).
When i am in the https://hello.friday.de/quote/selectFuelType, (please find the image)
I am not able to click on the item (Benzin) with the below xpath:
final String fuel = "//*[#id=\"root\"]/div/div[3]/div/div[2]/div/div/form/div[2]/div[2]/button[1]";
During the runtime, there is no error, so, i expect that I can successfully click on the element, or successfully get the current url as /selectFuelType but none of them are working.
The problematic method is the i_am_asked_to_specify_the_Fuel_Type_of_the_car() in the RegisterInsuranceSteps class.
Your xpath can be much simpler and stabler. Try this:
final String fuel = "//button[contains(., 'Benzin')]"
See more details about contains method at MDN web docs or check this SO answer to see how to check against attributes or tag names.
Regarding verifying if button was really click, I think it's enough if your next test step fails. In your case it would be choosing the engine power.
On the website https://hello.friday.de/quote/selectPrecondition the first option Das Auto ist schon versichert is selected by default. So click() the second option Das Auto wird noch zugelassen oder umgemeldet you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class^='RadioButtonListField__container--'] button:nth-child(2)"))).click()
XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(#class, 'RadioButtonListField__container--')]//following-sibling::button[1]"))).click();
Note: These are relative locators as per the relevant HTML and are not hard-coded to the exact text of the options.
Firstly if selenium doesnt click then try to use alternate xpath. You can also use css selectors.
Secondly if you want to check if click has happened and next page is loaded then you can simply check change in title or you can check if some element has loaded from next page.

Selenium WebDriver "java", I can not click on button on footer

The scenario is:
Try to add experience in linkedin.
Then click on save button to save the added experience.
The below is html code for this button:
<button class="pe-form-footer__action--submit form-submit-action Sans-15px-white-100%" type="submit">
Save
</button>
I am trying to find it by xpath using:
#FindBy (xpath = "//*[contains(text(), 'Save')]")
WebElement saveExperienceButton;
Following screenshot may help:
I will appreciate your help.
if you do not mind css/xpath selectors that do not look very elegant, you can always open up Chrome developer tools on the website you wanna test with Selenium, mark the DOM elements you wanna access and in the context menu choose 'Copy xpath' or 'copy selector':
Try this xpath:
(//*[text()='Save'])[2]
On my profile there are 2 Save buttons - the second one is the skill save. Also, you might want to check this question for the contains syntax.
Creating an XPath using text is a less preferable way. instead of that use other attribute value which is unique.
for ex: in your case
//footer//*[contains(#class, 'form-submit')]

How to locate this <a> element with Selenium?

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.

How to get value from <h1> tag using class in Selenium WebDriver, Java

I have html code:
<div class="formCaptionContainer fill-width" data-dyn-bind="sizing: { width: $dyn.layout.Size.available }">
<h1 class="formCaption" data-dyn-bind="text: $data.Caption, click: $data.GoGridView">Expense report for Aditi Mehta - GS1-000282, testing2</h1>
<h2 class="formCaption-context" data-dyn-bind="text: $data.ParentTitleFields">Aditi Mehta : GS1-000282</h2>
</div>
I want to get the value Expense report for Aditi Mehta - GS1-000282, testing2 from /h1 tag
Any one know how to do it?
I've tried :
By.xpath(".//div[#class='formCaption']/h1";
Above showing no element found
By.className("formCaption");
Above showing blank data
By.xpath(".//*[#class='formCaption']/h1");
Above showing no element found
This code work for me very well
String textprint=driver.findElement(By.xpath("//h2[#class='formCaption-context']")).getText();
System.out.println(textprint);
Hope It will solve your problem
Try this one
String msg= driver.findElement(By.xpath(//div[contains(#class='formCaptionContainer')]/h1 ).getText();
Can you try the below Xpath.
String msg=driver.findElement(By.xpath("//div[#class='formCaptionContainer fill-width']/h1[#class='formCaption-context']")).getText();
As per the HTML you have shared, the node attributes looks dynamic to me. So we have to induce WebDriverWait as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//h1[#class='formCaption']"), "Aditi Mehta - GS1-"));
System.out.println(driver.findElement(By.xpath("//h1[#class='formCaption']")).getAttribute("innerHTML"));
You need to consider your locator strategy, the following provide good preferences for general case approach.
location by ID globally unique elements
locate by name for page unique elements
locate by css as catch all case
You should also take a look at the PageObject pattern.
It appears the page is using a JS library to display the contents through late binding/resolution. Your test needs allow for this and wait until the page elements you are interested in are fully rendered. Find out from the developers what the element looks like before it is resolved and afterwards. Late resolution is one considerations in determining your locator strategy. The strategy should include fluent waiting and default time-outs for elements to appear.
This is not a problem of fixing a single locator.
Thanks all for your help. It is working fine now. I use the below code
By headingExpenseText = By.xpath("//*[#class='formCaption'][contains(text(),'Expense report for')]");
public String getTitleEx()
{
return driver.findElement(headingExpenseText).getAttribute("innerHTML");
}
This worked for me:
String titleElem = driver.findElement(By.xpath("//*[#class='formCaptionContainer fill-width']/h1")).getAttribute("innerHTML");
assertEquals("Worked", titleElem);
What do you need exactly, Data displayed on web page or textValue used in HTML code?
If your locator is correct , then instead of getText(), you can try getAttibute("innerHTML") like below,
By.xpath("//*[#class='formCaption'][contains(text(),'Aditi Mehta')]").getAttribute("innerHTML");

How can I map the XPath in Selenium?

I am currently trying to map the highlighted line shown below in the screen grab.
I've looked at w3schools and looked it up here on SO but cant seem to get my code right.. My selenium script keeps erroring out with cannot identify element.
currently, I am doing something like:
selenium.click("xpath=//table[#id='resultTable']/tbody/tr[#class='level3']/td[#id='resultTable_0_0_1_ob']/span/a[#class='linkOnly']");
I have also tried this:
selenium.click("xpath=//table[#id='resultTable']/tbody[1]/tr[6]/td[2]/span[1]/a[1]");
and this:
selenium.click("xpath=//table[#id='resultTable']/tbody/tr[6]/td[2]/span/a");
Am I doing it right and I just need to put in a delay ? or am I doing this completely wrong ?
EDIT:
Here is the code snippet as requested. Thanks for the pointer, I didn't really notice that there were multiple classes with the same name! Hm, but for some reason, the other two XPaths that I wrote dont work.
In this code snippet, I expanded the table so you can better see how the table is set up. Again, sorry for the image size, but ctrl+scroll up should enlarge the picture.
I'd prefer using css selectors as they work faster (currently using webdriver + java)
so solution to your problem be like:
String cssSelector = "tr[class='level3']>td[id='resultTable_0_0_1_ob']>span>a[class='linkOnly']"
driver.findElement(By.cssSelector(cssSelector)).click():
driver.manage.timeouts.implicitWait(3,TimeUnit.SECONDS);
As you got ID='resultTable_0_0_1_ob' I think this ID should help in finding unique element on the page.
2nd way to solve your problem:
also if framework which your site is implemented on supports jQuery you can easly use jQuery:
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $("tr[class='level3']>td[id='resultTable_0_0_1_ob']>span>a[class='linkOnly'];");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
And don't forget to verify your xpaths, css selectors in firepath(firebug extension) in firefox.
Picture below provided:
Hope this helps you.
xpath=//table[#id='resultTable']/tbody/tr[#class='level3']/td[#id='resultTable_0_0_1_ob']/span/a[#class='linkOnly']
The Corresponding CSS is css=#resultTable > tbody > tr.level3 > #resultTable_0_0_1_ob > span > .linkOnly

Categories

Resources