Selenium WebDriver -- get <span> text - java

I need to get the price value $726.35 from:
<span id="paiement-resultats"class="calculateur-resultats-total" style="" xpath="1">$726.35</span>
but it doesn't work:
driver.findElement(By.id("paiement-resultats")).getText()
How can I get this value?
Screenshot from the browser:

As the element is within a <span> tag, to extract the text $726.35 you can use either of the following solution:
cssSelector:
driver.findElement(By.cssSelector("span.calculateur-resultats-total#paiement-resultats")).getText();
xpath:
driver.findElement(By.xpath("//span[#class='calculateur-resultats-total' and #id='paiement-resultats']")).getText();
Update
As the results were unstable you can induce WebDriverWait for the visibilityOfElementLocated and you can use either of the following solutions:
cssSelector:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[aria-label='User']"))).getAttribute("innerHTML");
xpath:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[contains(#style,'vertbar')]"))).getAttribute("innerHTML");

Try this one,
Below i have Mentioned Two Xpath Try With that, if not working Let Me Know
//div[#id='resultats']//following::span[#id='paiement-resultats']
//div[#id='resultats']//following::span[#class='calculateur-resultats-total']
driver.findElement(By.xpath("//h2[#class='resultats']//following::span[#id='paiement-resultats']")).getText()

Cheers!
I found the solution
driver.findElement(By.xpath("//span[#class='calculateur-resultats-total' and contains(text(),'$')]")).getText();

Related

How to send text to elements without id or value attribute using Selenium Java

HTML:
<input type="text" placeholder="Enter name" class="form-control m-2">
I am trying to send text to this through Selenium.
This is what I tried:
driver.findElement(By.cssSelector("input.form-control m-2[placeholder='Enter name']")).sendKeys("Test");
form-control and m-2 are two different classes.
The selector should be input.form-control.m-2[placeholder='Enter name']
Use xpath to interact with the web Element.
The xpath will be -
//input[#placeholder='Enter name'][#type='text']
Use the following code -
WebElement inputName = driver.findElement(By.xpath("//input[#placeholder='Enter name'][#type='text']"));
inputName.sendKeys("Test");
To send a character sequence to the desired element with placeholder as Enter name you can use either of the following Locator Strategies:
cssSelector:
driver.findElement(By.cssSelector("input[placeholder='Enter name']")).sendKeys("Test");
xpath:
driver.findElement(By.xpath("//input[#placeholder='Enter name']")).sendKeys("Test");
Ideally, to send a character sequence within the <input> element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:
cssSelector:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[placeholder='Enter name']"))).sendKeys("Test");
xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#placeholder='Enter name']"))).sendKeys("Test");

How to write code for linkText for the html code

I am new to selenium and trying to write linktext code for the html code
Programmer
Code trial:
driver.findElement(By.linkText("Programmer")).click();
the code above is not working, is something wrong?
linkText should work if the element with the text Programmer is uniquely identified within the HTML. Still as the element is a JavaScript enabled element so to click() on the element you need to use elementToBeClickable() and you can use the following Locator Strategy:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Programmer"))).click();
Alternative
As an alternative you can also use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.labels[href^='repopulateUser']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='labels' and starts-with(#href, 'repopulateUser')][text()='Programmer']"))).click();

Trying to click on a hyperlink that includes spaces

I am trying to click on a hyperlink with a space using Java and Selenium. Here is a sample of the code
<h3 class="side menu">
<a class="side-menu" href="/configurations">
<span class="menu-icon ca"></span>Configuration
</a>
</h3>
I have tried using xpath starts-with and contains with no luck.
driver.findElement(By.xpath("//a[starts-with(text(),’Configuration’)]")).click();
To handle dynamic element use WebDriverWait and elementToBeClickable with expected conditions.Use the following Xpath locator strategies.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][normalize-space(.)="Configuration"]')));
element.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][contains(.,"Configuration")]')));
element.click()
I think all you need is to add a forward slash:
driver.findElement(By.xpath("//a[starts-with(text(),'/configuration')]")).click();
It works for me in the Chrome console if I type:
$x("//a[starts-with(#href,'/configuration')]")
I would use normalize-space when writing the xpath for the text, which will trim the leading and preceding white chars (spaces/tabs/newlines)
Here is how you should do it in your question.
//a[normalize-space(.)='Configuration']
Tested the xpath in chrome console for click() successfully using javascript.

How to click on Log In button/link data-testid using Selenium and Java

I am attempting to click on 'Log in' but my Selenium code isn't working. Below is the HTML code.
<span class="css-14krylx-text-text-fullPageText-FormFooter">Already have a Times account?
<span tabindex="0" data-testid="switch-to-login" class="css-dip6gw-link-link-FormFooter">Log in</span></span>
What I attempted so far and did not work?
driver.findElement(By.cssSelector("[data-testid='switch-to-login'")).click();
driver.findElement(By.xpath("//span[#data-testid='switch-to-login'")).click();
driver.findElement(By.cssSelector("/.css-dip6gw-link-link-FormFooter'")).click();
Is there any other approach?
The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span[class$='text-text-fullPageText-FormFooter'] span[class$='link-link-FormFooter'][data-testid='switch-to-login']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#class, 'text-text-fullPageText-FormFooter')]//span[contains(#class, 'link-link-FormFooter') and text()='Log in']"))).click();

Unable to locate the element as per the below anchor tag

Please advise how to locate the link for New business tag in the below code. I have tried the following xpath but it didnt work:
driver.findElement(By.linkText("NEW BUSINESS")).click
driver.findElement(By.xpath("//span[#class='hdBottomBar']/a[1]"))
HTML:
<span class="hdBottomBar">
<a class="hdTopBar" href="javascript: void navCntl('NewBusiness','NavBar');" onmouseover="window.status='New Business';return true" onmouseout="window.status='';return true" name="newBusiness">NEW BUSINESS</a>
The element is a JavaScript enabled element so to invoke click() you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("NEW BUSINESS"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.hdBottomBar>a.hdTopBar[name='newBusiness']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='hdBottomBar']/a[#class='hdTopBar' and #name='newBusiness'][text()='NEW BUSINESS']"))).click();
try with:
//span[#class='hdBottomBar']/a[#name='newBusiness']
or
//span[#class='hdBottomBar']/a[text()='NEW BUSINESS']

Categories

Resources