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;
Related
How to click on a specific text of search text box results in selenium WebDriver. In my case search text box is "School".
I'm sending keys "RGSchool1" in the text box "School" and then I want to click on "RGScool" when it shows up as results under the text box.
I tried all below approached by it's throwing "org.openqa.selenium.NoSuchElementException"
Enter text and tab out
Enter text and send enter key
Absolute path - /html1/body1/div[7]/ul1/li1/div1/span1
Relative path - //span[#class='select2-match']
HTML body:
<div class="select2-result-label" style="" xpath="1">
<span class="select2-match">RGSchoo</span>
l1 [rgschool1]</div>
Code:
//Finding elements
#FindBy(id = "s2id_User_OrgId")
public WebElement clickJurisdiction;
#FindBy(xpath = "/html[1]/body[1]/div[6]/div[1]/input[1]")
public WebElement keyInJurisdiction;
#FindBy(xpath = "//div[#id='s2id_User_OrgUnitId']//a[#class='select2- choice']")
public WebElement clickSchool;
#FindBy(xpath = "/html[1]/body[1]/div[7]/div[1]/input[1]")
public WebElement keyInSchool;
#FindBy(xpath = "/html[1]/body[1]/div[7]/ul[1]/li[1]/div[1]")
public WebElement schoolSearchResult2;
Call method:
public void enterNewUserData() {
SeleniumTestHelper.enterText(firstName, Config.getProperty("FirstName"));
SeleniumTestHelper.enterText(middleName, Config.getProperty("MiddleName"));
SeleniumTestHelper.enterText(lastName, Config.getProperty("LastName"));
SeleniumTestHelper.enterText(preferredName, Config.getProperty("PreferredName"));
SeleniumTestHelper.clickOnButton(clickJurisdiction);
SeleniumTestHelper.enterText(keyInJurisdiction, Config.getProperty("Jurisdiction"));
SeleniumTestHelper.enter(keyInJurisdiction);
SeleniumTestHelper.clickOnButton(clickSchool);
SeleniumTestHelper.enterText(keyInSchool, Config.getProperty("School"));
SeleniumTestHelper.clickOnButton(schoolSearchResult2); // It fails here
Please help me to find a solution. I'm new to this kind of scenario.
Please see the below-attached screenshot.
Screenshot before entering data:
Screenshot after entering data
To locate the element with text as RGSchool1 you can use the following Locator Strategy:
Using cssSelector:
#FindBy(cssSelector = "ul.select2-results div.select2-result-label > span.select2-match")
public WebElement schoolSearchResult2;
Using xpath:
#FindBy(xpath = "//ul[#class='select2-results']//div[#class='select2-result-label']/span[#class='select2-match']")
public WebElement schoolSearchResult2;
HTML:
"Add to cart" object
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="ProductPrimaryCTA-cta_add_to_cart_button" type="button"><span class="button-wrapper"><span class="spin-button-children">Add to cart</span></span></button>
"Get in stock alert" object
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="cta_oos_button" aria-expanded="false" aria-label="get in stock alert" role="button" tabindex="0" type="button"><span class="button-wrapper"><span class="spin-button-children">Get in-stock alert</span></span></button>
I do not want to click "Add to cart". I just want to store locator info in WebElement "addToCart". Because objects have very identical properties, I chose this unique property "data-tl-id" but did not work.
Web Driver
WebElement addToCart = driver.findElement(By.xpath("//button[#data-tl-id='ProductPrimaryCTA-cta_add_to_cart_button']"));
Below worked for me but both above objects have the same class name so i cant use the class name.
WebElement addToCart = driver.findElement(By.xpath("//span[#class='button-wrapper']"));
I also tried below ways but none worked for me.
WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
WebElement addToCart = driver.findElement(By.xpath("//span[contains(#text,'Add to cart')]"));
I get error "No such element: Unable to locate element: ". How to locate the "Add to cart" object?
Your xpath is wrong WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
Try below solution
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Add to cart']")));
System.out.println("Printing "+webElement.getText());
Or you can also try xpath with contains
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'Add to cart')]")));
System.out.println("Printing "+webElement.getText());
since this xpath worked for you,
WebElement addToCart = driver.findElement(By.xpath("//span[#class='button-wrapper']"));
Use this to uniquely identify WebElement
driver.findElement(By.xpath("//button[contains(#data-tl-id,'cart')]/span[#class='button-wrapper']"))
you can also use this xpath
//span[contains(text(),'Add to cart')]
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");
}
Could anyone please help me to hit the javascript link through selenium code.
This is the inspect code and link name is Applications.
Currently I am using below all the list of code, but still it's not hitting the applications link.
driver.findElement(By.id("I6")).click();
driver.findElement(By.xpath("//img[# src='/ibm/console/images/arrow_collapsed.gif']")).click();
driver.findElement(By.xpath("//span[contains(text(),'Applications']")).click();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.xpath("//*[contains(text(),Applications')]"));
js.executeScript("arguments[0].click;", element);
WebElement element1 = driver.findElement(By.xpath("//span[contains(text(),'Applications']"));
element1.click();
driver.findElement(By.xpath("//a[#href='javascript:expandCollapse('6');]")).click();
driver.findElement(By.xpath("//div[#id='I6']/..//a[contains(text(),'Applications')]")).click();
Try to click WebElement using javascript this way.
WebElement element = driver.findElement(By.xpath("//span[contains(text(),'Applications']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
I have try with your this html code:
<img src="/ibm/console/images/arrow_expanded.gif" title="Collapse" alt="Collapse" id="I6" border="0" align="absmiddle"> <img src="/ibm/console/images/arrow_collapsed.gif" title="Expand" alt="Expand" id="I6" border="0" align="absmiddle"> <a style="color:#000000;text-decoration:none;" href="javascript:expandCollapse('6');" title="Applications"><img src="/ibm/console/images/arrow_collapsed.gif" title="Expand" alt="Expand" id="I6" border="0" align="absmiddle"><span dir="ltr">Applications</span></a>
Refer Image for more details:
Correct me if I'm wrong, there is typo in all of your xpath like :
driver.findElement(By.xpath("//span[contains(text(),'Applications']")).click(); \\missed `)` in contains method
If see carefully somewhere ' missing and somewhere () , Ok leave it, try the following xpath and let us know still face same issue
driver.findElement(By.xpath("//a[#title='Applications']")).click();
or
driver.findElement(By.xpath("//a[contains(.,'Applications')]")).click();
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"