How to get following text name using xpath? - java

I try with following xpath
driver.findElement(By.xpath("//div[contains(#class,'ReactTags__selected')]//span[1]/text()"));
but get following error
"org.openqa.selenium.InvalidSelectorException: invalid selector: The result of the xpath expression "//div[contains(#class,'ReactTags__selected')]//span[2]/text()" is: [object Text]. It should be an element."
<div class="ReactTags__selected">
<span class="tag-wrapper ReactTags__tag" style="opacity: 1; cursor: auto;" draggable="true">
"TestName"
<a class="ReactTags__remove">×</a>
</span>
</div>

Please try the below Xpath.
Hope it helps
//div[contains(#class,'ReactTags__selected')]//span[2]/a/text()

text() in the xpath returns text node, Selenium doesn't support it. Locate the element and use getText() to get the text
WebElement element = driver.findElement(By.xpath("//div[contains(#class,'ReactTags__selected')]//span[1]"));
String text = element.getText();

As the parent element contains the attribute draggable="true" invariably it's a dynamic element, precisely React element. Additionally as the text TestName is a Text Node you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
cssSelector:
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].firstChild.textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ReactTags__selected > span.tag-wrapper.ReactTags__tag")))).toString());
xpath:
System.out.println((String)((JavaScriptExecutor)driver).executeScript("return arguments[0].firstChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='ReactTags__selected']/span[#class='tag-wrapper ReactTags__tag']")))));

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 get the text from <span> tag and use this text on other page using Selenium and Java

I want to get text included in a span tag.
Here's HTML
<div class="position">
<h1>
<span class="select-text">Some Text</span>
</h1>
</div>
And I've tried this
wd.findElement(By.xpath("//div[#class="position"]//h1//span")).getText();
But I get this error all the time and IDK what I'm doing wrong
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class="position"]//h1//span"}
Additional problem: after getting a text from this page I need to make sure if this text is not visible on another page. If you have any idea how can I realize it I'll be grateful.
I believe you will find that your xpath selector for div with class 'position' should be
div[contains(#class, 'position')]
To print the text Some Text from the <span> you can use either of the following Locator Strategies:
Using cssSelector and getAttribute("innerHTML"):
System.out.println(wd.findElement(By.cssSelector("div.position>h1>span.select-text")).getAttribute("innerHTML"));
Using xpath and getText():
System.out.println(wd.findElement(By.xpath("//div[#class='position']/h1/span[#class='select-text']")).getText());
Ideally, to extract the text Some Text as the element is a dynamic element, you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:
Using cssSelector and getText():
System.out.println(new WebDriverWait(wd, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.position>h1>span.select-text"))).getText());
Using xpath and getAttribute("innerHTML"):
System.out.println(new WebDriverWait(wd, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='position']/h1/span[#class='select-text']"))).getAttribute("innerHTML"));
Try this xpath:
wd.findElement(By.xpath("//div[contains(#class='position')]/h1/span[contains(text(), 'Some Text') ]")).getText () ;
If you need to check, if this text exists in another page or not, you need to write the generic xpath, which will capture the results.
Compare your current string with another text using if loop.

How to use driver.findElement to locate an element with below XML?

I have XML as below:
<input name="_jpfcpncuivr___ns535482039__j_id__ctru0:fragmentRegionStatic:0:it1" maxlength="6" style="text-transform:uppercase" type="text" class="af_inputText_content" id="_jpfcpncuivr___ns535482039__j_id__ctru0:fragmentRegionStatic:0:it1::content">
I have tried driver.findElement by id, name, className, cssSelector, or xpath but all failed and got an error:
no such element: Unable to locate element:
The desired element is a dynamic element so to locate and click() on the element you need 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("input[class$='inputText_content'][id*='fragmentRegionStatic']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#class, 'inputText_content') and contains(#id, 'fragmentRegionStatic')]"))).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']

Selenium WebDriver -- get <span> text

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

Categories

Resources