<div class="col-lg-3 col-sm-4 col-xs-6">
<div class="logo">..................</div> </div>
logo is inner div.
I need to acces col-lg-3 col... div content using xpath.
I did this in java:
WebElement mydiv = driver.findElement(By.className("col-lg-3 ")) ;
but it does not work - because it has spaces in the name.
How do I access this using xpath?
In CSS, you could access it like this:
driver.findElement(By.cssSelector(".col-lg-3 .logo"));
Since you asked, in XPath, it could be:
driver.findElement(By.xpath("//div[contains(#class, 'col-lg-3')]/div[contains(#class, 'logo')]")
As you can see, the CSS selector is much simpler.
To see xpath, I suggest to install Firebug and FirePath; those extensions are Firefox.
Example:
Can you see the image? xpath
Imagine that you don't have id neither name locators or you need to use xpath
Open Firefox.
Click on "Firebug". (#1)
There a panel.
Click on "Click an element in the page to inspect" icon (#2)
Click on "FirePath" tab. (#3)
Select the element that you wish (#4)
It displays the xpath into textbox. (#5)
So, you need to add this line:
driver.findElement(By.xpath("html/body/form/fieldset/p[1]/input"));
Related
I've gotten one of the more frustrating Selenium problems.
I've got a table on a page, where one of the elements have a link. The link is called "Send brev".
<td data-e2e-selector="sakHendelser" id="sakHendelser_0" class="ng-star-inserted">
<saksnytt><!----><!----><!---->
<div class="hb-tekst--ingenBryting ng-star-inserted">
<!----><!----><!---->
<button class="hb-knapp hb-knapp--lenke hb-knapp--alignTeks hb-knapp-lenke-nopadding ng-star-inserted"
data-e2e-selector="saksnytt-link">
<i aria-hidden="true" class="fa fa-flag fa-envelope"></i>
<span class="hb-knapp-tekst">Send brev </span></button>
<!----><span aria-hidden="true" class="fa fa-info-circle ng-star-inserted"
id="reservert-info">
</span><!----><!----><!----></div><!---->
</saksnytt></td>
This has worked before, and I haven't found a reason for why it's stopped working now. But no matter how I try to find it, Selenium responds with
no such element: Unable to locate element: {"method":"xpath","selector":"//data-e2e-selector[contains(text(),'Send brev ')]"}
or similar-
I've tried the following:
Browser.Wait().until(presenceOfElementLocated(By.linkText(merknad + " ")));
This times out.
driver.findElement(By.linkText(merknad + " ")).click();
driver.findElement(By.partialLinkText(merknad + " ")).click();
driver.findElement(By.xpath("//data-e2e-selector[contains(text(),'" + "Send brev" +"')]"));
driver.findElement(By.xpath("//id[contains(text(),'" + "Send brev" +"')]"));
data-e2e-selector="sakHendelser" id="sakHendelser
I've also tried adding a space after the "v" in the link text, with no luck.
How can Selenium not find an element that is clearly visible and interactable like this?
Since the code is generated in Angular, and is more or less dynamic, I cannot create a tag for this specific element either, so I'm left with trying to find it by text. Which I cannot get to work.
Any ideas?
I AM able to click it with this code:
driver.findElement(By.xpath("//*[#id='sakHendelser_0']/saksnytt/div/button/span")).click();
But I want to be able to send the link text to the method instead of hardcoding the xpath like that.
LinkText and PartialLinkText will only work with text inside an A tag. You don't have that given your HTML so this will not work. Your only option to locate an element given the contained text is to use XPath.
The way you are using XPath by placing // before an attribute (such as //id or //data-e2e-selector) is not correct. These are element attributes, not tags.
The text Send brev is contained in a span element, so trying to locate it by querying on data-e2e-selector will not work -- that's also not a WebElement, it's an attribute on button.
Because you mention this item has some visible link text that you want to use as a parameter, I would query on only text and use a contains to work around any hidden whitespace:
driver.findElement(By.xpath("//span[contains(text(), 'Send brev')]")).click();
You can use link text as a parameter as such:
string linkText = "Send brev"
driver.findElement(By.xpath("//span[contains(text(), '" + linkText + "')]")).click();
You might have better luck clicking on the button itself though:
driver.findElement(By.xpath("//button[span[contains(text(), '" + linkText + "')]]")).click();
Try using normalize-space with xpath:
//span[normalize-space(text())='Send brev']
You need to take care of a couple of things as follows:
By.linkText() and By.partialLinkText() works only for <a> tags, where as the desired element is within a <span> tag.
data-e2e-selector and id are attributes of an WebElement but you have tried to use them as tagName.
The text Send brev is with in a <span> tag.
So identify the element with text as Send brev and moving ahead to interact with it you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.hb-knapp.hb-knapp--lenke.hb-knapp--alignTeks.hb-knapp-lenke-nopadding.ng-star-inserted[data-e2e-selector='saksnytt-link'] span.hb-knapp-tekst"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='hb-knapp hb-knapp--lenke hb-knapp--alignTeks hb-knapp-lenke-nopadding ng-star-inserted' and #data-e2e-selector='saksnytt-link']//span[#class='hb-knapp-tekst' and contains(., 'Send brev')]"))).click();
i should make Selenium to click on the element of drop down menu using Java and Inteliji. I should click on the "today" button. I tried to copy the xpath, use cssselector, i used extensions like xpath finder etc, no result. The element is <li> type, so i guess the problem is here. Any suggestions how to find the correct Xpath?
P.S. sorry for uploading the image, as a new user, i can't put them exactly in the text.
Drop down menu image
html code for the elements
You can't always get reusable XPath locator for selenium from the browser's tool. It returns an absolute XPath. You need to construct relative XPath for the elements.
Here you can learn about XPath and how XPath locators work.
The following locators based on the image you have posted.
XPath:
WebElement liToday = driver.findElement(By.xpath("//div[contains(#class,'daterangepicker') and contains(#class,'dropdown-menu')]/div[#class='ranges']/ul/li[text()='Today']"));
CSS Selector:
WebElement liToday = driver.findElement(By.cssSelector("div.daterangepicker.dropdown-menu > div.ranges > ul > li"));
After locating the element,
this part is for after you have clicked the date box and the dropdown is showing.
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(liToday));
liToday.click();
I am new to xpaths in selenium and trying to click on Next> image/button in the code below. I have tried following two xpaths but its not working and giving no element not found error.
By.xpath("//div[#class='pzbtn-mid']/img[contains(text(), \"Next >\")]"))
By.xpath("//div[#class='pzbtn-mid']/img[contains(text(), 'Next >')]"))
What am i doing wrong here?
<div class="pzbtn-mid" data-bindprops="innerHTML" data-click="...."> ==$0
<img src="webwb/zblankimage.gif" alt="" class="pzbtn-i">
"Next >"
<img alt="" src="webwb/zblankimage.gif" class="pzbtn-i">
As per the HTML you have shared to click() on the Next> image/button you can use the following xpath :
By.xpath("//div[#class='pzbtn-mid']/img[#class='pzbtn-i' and #src='webwb/zblankimage.gif']"))
Note : The text Next > is not within any of the <img> tags but within the <div> tag. Hence to click on the image/button you have to reach till the <img> tag.
You can also use xpath below,
By.xpath("//div[contains(#class,'pzbtn-mid') and contains(.,'Next >')]//img")
It clicks the first image if the img belongs to next. Othervise you should click the second img like below;
driver.findElementsBy((By.xpath("above xpath")).get(1));
I am in a situation where there are no unique id and there are number of div's under a class. Cssselector and xpath's are so generic that they are not being recognized.
This is what the Html looks like:
This is my code which doesn't work:
#Test
public void NaviToEpisode(){
driver.findElement(By.linkText("/episode")).click();
title_episode = driver.getTitle();
Assert.assertTrue(title_episode.contains("File uploading"));
}
Please help!
You can use cssSelector, in your case it would be:
driver.findElement(By.cssSelector("#links>div>a").click();
If you use Firefox, install Firebug plugin, then right click on the element you wish to inspect and in menu click on "Inspect with Firebug", once the snippet of your code highlighted right click on it and you should see an option to copy xpath or css.
try this driver.findElement(By.xpath("//*[contains(#href, '/episode/')]")).click();
<div id="links" . . > seems to be static I hope it's unique as well. Following css selector can be used to select first link (i.e. /episodes/)
#links div:nth-child(1) a
Similarly you can use css selectors to select sub-sequent elements. For example to select 2nd element:
#links div:nth-child(2) a
So instead of using By.linkText("/episode"), use By.cssSelector("#links div:nth-child(1) a").
Here is the HTML:
<li>
<input type="checkbox" checked="" name="selectedMstrPrivGroupList[9].mstrAuthorities[0].status"/>
Add Dexter
</li>
How could this element be clicked in WebDriver? It is a check box. And I want to use XPath as I have close to 30+ check boxes in the page. So that I can create a generic method and pass only the WebElement. I tried the following but didn't work.
Driver.findElement(By.xpath("//input[contains(.,'Add Dexter')]")).click();
If the checkbox next to "Add Dexter" is what you want to click on the page, you can use:
Driver.findElement(By.xpath("//li[contains(.,'Add Dexter')]//input[#type='checkbox']")).click();
What is with this one:
Driver.findElement(By.xpath("//input[#name='selectedMstrPrivGroupList[9].mstrAuthorities[0].status']")).click();
You can use like this,
driver.findElement(By.xpath("//li[contains(text(),'Add Dexter')]")).click()
You can use xpath to click on the element as below:
driver.findElement(By.xpath("//input[text()='Add Dexter']")).click();
You can also click on that element by using cssSelector instead of xpath as below:
driver.findElement(By.cssSelector("input:contains(^Add Dexter$)")).click();
Note: CssPath/CssSelector is faster than xpath. So it's better to use cssSelector than xpath in most cases.