java - Extract text which contains <br> tag with Selenium Webdriver
Problem resolved by using
String text = driver.findElement(By.tagName("div")).getAttribute("innerText");
and not
String text = driver.findElement(By.tagName("div")).getAttribute("textContent");
Extract text br tags in selenium JAVA
If you want to click on preceding link you can use below code:
findElement(By.xpath("//text()[contains(., 'Texas-United States')]/preceding-sibling::a[1]")).click()
and
findElement(By.xpath("//text()[contains(., 'Illinois-United States')]/preceding-sibling::a[1]")).click()
extract data before and after <br> tag using selenium
you can use getText()
which will return entire element text. For <Br>
it will have line break in returning text so you can spilt text by '\n'
if want each line text.
How to use xpath for extracting text between <br/> tag
Alternative solution using normalize-space()
//span[@class="ms-cui-ctl-largelabel" and normalize-space()="New Map"]
How to extract partial text from an element seperated by <br> tag through selenium?
That Xpath is crazy, but to address your issue I think it might be as simple as adding a space between the quotes in your split() function. It should be
REP=Repor.split(“ “)
Edit after clarification: the string Repor is being set with a line break. Thus the following code should find "70" and "(100%)" individually.
String lines[] = Repor.split("\\r?\\n");
then lines[0]
is "70" and lines[1]
is "(100%)". Is this what you wanted?
get text from element with <br> on its composition, using Python Selenium
Try to get list of text nodes as
driver.find_element_by_xpath('//*[@id="nomapdata"]/div/div/div/div[2]/div[1]').text.split("\n")
If there are more text nodes after phone number which you don't want to use:
driver.find_element_by_xpath('//*[@id="nomapdata"]/div/div/div/div[2]/div[1]').text.split("\n")[:4]
Extract Text from Website without any HTML tags with selenium
You can use the below xpath:
//strong[text()='Customer']/../following-sibling::div/descendant::div[starts-with(@class,'col-md-10')]
like this:
String gottenText = new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//strong[text()='Customer']/../following-sibling::div/descendant::div[@class='col-md-10']"))).getAttribute("innerText");
System.out.println(gottenText);
that shall provide the below output:
Email user@phptravels.com
Password demouser
Update:
you can store them into String variable like below:
String gottenText = new WebDriverWait(driver, Duration.ofSeconds(20)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//strong[text()='Customer']/../following-sibling::div/descendant::div[@class='col-md-10']"))).getAttribute("innerText");
String[] arr = gottenText.split(" ");
String[] userNames = arr[1].split("\\r?\\n");
String userName = userNames[0];
String password = arr[2];
System.out.println(userName + " " + password);
WebDriver getText() Method with Replace <br> Tag
You need to remove the new line character, try this :
status.replaceAll("\\r\\n|\\r|\\n", " ");
It will remove all line breaks with space.
Related Topics
No String-Argument Constructor/Factory Method to Deserialize from String Value ('')
Could Not Extract Resultset When Performing Customized Native Query
Method in Class Cannot Be Applied to Given Types. Required: No Arguments
Java 8 Streams: Multiple Filters Vs. Complex Condition
Bootstrap.Yml Not Loading in Spring Boot 2
Chrome Browser Is Not Opening in Selenium Webdriver
How to Handle Incorrect User Input into a Scanner
Selenium Webdriver: Modifying Navigator.Webdriver Flag to Prevent Selenium Detection
How to Run Java Program in Terminal With External Library Jar
How to Store User Input into an Existing Array
In Java: How to Zip File from Byte[] Array
Eclipse: the Resource Is Not on the Build Path of a Java Project
Forwarding the Request from One Jsp to Another Jsp With All Request Parameters
Regular Expression to Remove Everything But Characters and Numbers
Using Streams to Convert a List of Objects into a String Obtained from the Tostring Method