Extract Text Br Tags in Selenium Java

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



Leave a reply



Submit