I am trying to identify a WebElement in Mozilla browser using the xpath. Below is the html code:
<div id="address-book" class="grid-12" style="display:none;">
<!-- END ADDRESS BOOK -->
<!-- BEGIN PAYMENT OPTIONS -->
<div id="paymentSection" class="grid-12 form-section">
<div class="grid-contentx">
<div class="hd header-container">
<h3>Payment Information</h3>
</div>
</div>
<!-- BEGIN: CC FORMS -->
<div class="grid-6">
The relevant xpath I wrote in the page object factory is:
#FindBy(xpath = "//*[#id='paymentSection']/div[1]/div/h3")
private WebElement paysection;
Upon running the script, I am getting an error message "unable to identify the element". Please help me if there is any correction to be done to the identified xpath.
There are many xpaths which can help you locate h3 in the above case, let me list down a few :
using id :
xpath = "//div[#id='paymentSection']//h3"
using text :
xpath = "//h3[contains(.,'Payment Information')]"
using class names :
xpath = "//div[#id='paymentSection']//div[#class='hd header-container']/h3"
using combination of id and class name :
xpath = "//div[#id='paymentSection' and #class='grid-12 form-section']//h3"
Related
I have this html tree:
<div class="css-1dbjc4ns" data-testid="test-modal"></div>
<div class="css-1dbjc4n" data-testid="test-icon">
<div class="css-1dbjc4n"><div class="css-1dbjc4n"</div><div class="css-1dbjc4n r-
1wbh5a2 r-1peese0 r-6uxfom r-mbgqwd r-11c0sde r-1udh08x">
<div dir="auto" class="css-901oao ">This item is not yet available</div>
I am trying to locate the text "This item is not yet available".
This is my xpath:
#FindBy(xpath = "//div[contains(#data-testid, 'test-modal-container')"//contains(text(),This item is not yet available));
private WebElement itemComingSoonalert;
I am getting no such element present exception, can anyone please help? Thank you.
Try this xpath :
#FindBy(xpath = "//div[#data-testid='test-icon']/descendant::div[#dir='auto']");
private WebElement itemComingSoonalert;
or as request by OP :
#FindBy(xpath = "//div[#data-testid='test-icon']/descendant::div[#dir='auto' and text()='This item is not yet available']");
private WebElement itemComingSoonalert;
I have got null when I search for the src attribute of an image. Please resolve the element identification for the image.
My Selenium WebDriver code to identify the image src using WebDriver:
#FindBy(xpath = "//div[contains(#class, 'image-section')]/img")
private WebElement getimage;
public String getImageSrc(){
return getimage.getAttribute("src");
}
I have the following exception:
org.openqa.selenium.NoSuchElementException: no such element:
If this is your html:
<div class="image-section">
<div class="img-container">
<img alt="Test Alternate text" src="/content/dam/kh-facebook-group-cover- photo-size.png">
you could try this xpath in order to locate the element:
//div[#class='image-section']/div[#class='img-container']/img[#alt='Test Alternate text']
and after get the "src" attribute.
As per your comment below your question you can use any of this xpaths
//img[#alt='Test Alternate text']
//img[#alt='Test Alternate text' and #src='/content/dam/kh-facebook-group-cover- photo-size.png']
or
//div[#class='img-container']/img[#alt='Test Alternate text' and #src='/content/dam/kh-facebook-group-cover- photo-size.png']
Your HTML:
<div class="image-section">
<div class="img-container">
<img alt="Test Alternate text" src="/content/dam/kh-facebook-group-cover- photo-size.png">
What's wrong:
img tag is the second div tag
Solve it:
#FindBy(xpath = "//div[contains(#class, 'image-section')]//img")
private WebElement getimage;
public String getImageSrc(){
return getimage.getAttribute("src");
}
This is my sample html code.
<div class="content">
<M class="mclass">
<section id="sideA">
<div id="mainContent">
<div class="requestClass">
<span>Check</span>
<input type="text" id="box">
</div>
</div>
<section>
<section id="sideB">
...
<section>
</M>
</div>
I want to set some value to my text field ("box"). So I tired to set like below code
driver.findElement(By.xpath("...")).sendKeys("SetValue");
My Xpath id is correct, it's exist in the page but am getting this error
no such element: Unable to locate element: {"method":"xpath","selector":"id("..."}
Why I am getting this error because of my custom tag,if yes how to get element inside custom tag?
As per the HTML you have provided to fill in some value to the text field represented by <input type="text" id="box"> you can use either of the following line of code:
cssSelector :
driver.findElement(By.cssSelector("section#sideA input#box")).sendKeys("SetValue");
xpath :
driver.findElement(By.xpath("//section[#id='sideA']//input[#id='box']")).sendKeys("SetValue");
If you still want to use XPath. This worked for me-
driver.FindElement(By.XPath(#"//*[#id='box']")).SendKeys("AB");
I don't think the custom tag causes any problem as the CssSelector also works-
driver.FindElement(By.CssSelector(#"m[class='mclass'] input")).SendKeys("AB");
You can use ID or xpath to locate it, My suggestion you have to use ID. Also use Explicit wait till element to be visible.
Using ID, your code is like this:
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
elem.sendKeys("test");
You can also use JavascriptExecutor
WebElement elem= driver.findElement(By.id("box"));
WebDriverWait wait=new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
JavascriptExecutor myExecutor = ((JavascriptExecutor) driver);
myExecutor.executeScript("arguments[0].value='test';", elem);
I am trying to get the text under the tag. That is Arduus, Gstaad, Switzerland. I tried with the classname and also with xpath.
driver.findElement(By.className("chalet-details__title"));
String chaletTitle = driver.findElement(By.xpath("//div/h1[#class='chalet-details_title']")).getText();
But its giving NoSuchElementException. The classname has double underscore(__) .Is it because of that not working? Can anyone help me with this?
<div class="col-md-12 col-sm-12 col-xs-12">
<h1 class="chalet-details__title">
<span class="chalet-details__title-property">Arduus</span>,
<a class="chalet-details__title-resort" href="/ski- resorts/switzerland/gstaad">Gstaad</a>,
<a class="chalet-details__title-country" href="/ski- resorts/switzerland">Switzerland</a>
</h1>
<div class="chalet-details__rating">
<div class="chalet-details__wrapper">
<span class="chalet-details__star" style="width: 108px;"></span>
<span class="chalet-details__mask"></span>
</div>
</div>
</div>
You can try something like:
driver.findElement(by.cssSelector("h1.chalet-details__title"))
I just tried this and it worked fine with the provided html, if this still fails for you, can you verify that there aren't any iframes on the page.
Both your locators are OK, but you might need to wait for your element to be present in DOM:
WebDriverWait wait= new WebDriverWait(driver,20 );
String chaletTitle = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("chalet-details__title"))).getText();
Another reason for NoSuchElementException: your element could be located inside an iframe, so you need to switch to that frame before handling target element:
driver.switchTo().frame("iframeNameOrId");
If it has no attributes as id or name:
WebElement someFrame = driver.findElement(By.xpath("//iframe[#someAttr='someValue']"));
driver.switchTo().frame(someFrame);
To get all the text under h1 tag find all elements using locator as follows:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *")); //it will find all elements immediately under the h1 tag
Now, iterate over the elements.
Complete code:
List<WebElement> items = driver.findElements(By.cssSelector("h1.chalet-details__title > *"));
for(WebElement item : items){
System.out.println(item.getText());
}
Sample code:
<div class="loginbox">some code</div>
<div class="loginbox">other code</div>
<div class="loginbox">
<p> style="color: Red;">Test Extract</p>
</div>
Using Selenium Web Driver, I would like to extract the text Test Extract within the paragraph element which is nested within a div, whose class name is shared with other div classes. c# preferred.
You can try below method:
driver.findElement(By.xpath("//div[#class='loginbox']/p")).getText();
EDITED
You should use = inside the square braces like:
driver.findElement(By.xpath("//div[#class='loginbox']/p");
C# code to get the text from the locator specified,
IWebElement element = Browser.GetElementByCssSelector("div.loginbox p");
string text = element.Text;