How to select javascript tab using Selenium? - java

I'm trying to achieve a tab of Send sms, Hope this image will help you, what i'm looking for. Please see here.
So, here is source code:
<li id="sendSMS" class="active">
Send SMS
</li>
Now, I tried this source code:
driver.findElement(By.linkText("Send SMS")).click();
Unfortunately, This is NOT working :(
Please help, Surely help would be appreciated!

I don't usually rely on linkText that much Try using xpath with text based search
also, try this
//li[#id='sendSMS']/a
By byXpath = By.xpath("//a[.='Send SMS']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(byXpath);
myDynamicElement.click();
EDIT:
Added explicit wait

Related

How to click element under <a href> link using Selenium java

HTML code
using Selenium + Java. I need to click the filter button.
Currently, I am using below code but it doen't work.
WebElement filter = driver.findElement(By.linkText("Filter"));
filter.click();
Xpath - //*#id="frmBookingListing"]/div[3]/div1/div[3]/div/div1/div/div[2]/a[2]
cssSelector - .searchFilter
I don't know what's wrong with my code. please help to click on filter button.
Thanks in advance.
Try this:
.//a[#class='btn btn-typ4 searchFilter' and #title='Filter']/span

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 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");

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.

Select invalid html-tag with Selenium

I am trying to get a WebElement with Selenium:
driver.findElement(By.xpath("//input[#name='j_username']"))
But Selenium says: "Unable to find element with XPath ...".
The XPath is valid, I proofed it with FirePath.
But the input element has the following invalid code:
<input size="10" type="text" name="j_username" maxlength="8">
I can't change the html-file, despite the fact is there any solution to get the webElement?
Thanks in advance!
try select element with css selector. and also verify in firepath(firebug addon that element is located properly).
so your css selector be something like
input[name='j_username']
2nd approach is to use internal firebug mechanism for finding xPaths of elements.
See screen attached below
After these manipulations driver shoulda handle element properly.
Well I will suggest adding an id to your html code -
<input id="j_username"size="10" type="text" name="j_username" maxlength="8">
and findElement by id -
driver.findElement(By.id("j_username"));
I have faced similar issues with xpath(borwser issues??) but id never fails for me. ;)
By the way I feel your code should be -
driver.findElement(By.xpath(".//*[#name='j_username']"));
The best solution is to find out what selenium is doing wrong, but without a URL or sample page to test on it's a little hard. Is there anyway you could dump the HTML into a jsfiddle? If there is do that and paste the url into the question and I'm sure someone can find a solution.
If not however, another way to get the results is to do it with jQuery. If firebug is picking it up but not selenium, then there's no reason why jQuery wouldn't get it. Here's how to go about doing that if needed:
Step 1: Is jQuery already present on the page? If so then you don't need to do this bit, otherwise you will need to add it yourself by using driver.executeScript(addjQueryScript) where the script does something like this.
Step 2: call WebElement input = driver.executeScript(elementSelector); where the elementSelector script would be something like \"return $('input[name=\"j_username\"]')\");
My jQuery's not so good, but I believe that should work...
Best of luck!

Categories

Resources