Exclude text from script tag within the HTML DOM when calling element getText

0 votes

I'm using selenium on Java to test HTML page. Here's an example of HTML:

<a>
  Return to homepage
  <script>
    <form>...</form>
  </script>
</a>

I need to get text of <a> element to check that it's correct. Chrome, Firefox and IE returns me correct text (Return to homepage) when I call element.getText() for this <a> element, but Safari returns me also the text which is located inside <script> tag (which is not text, but HTML code)

Can anyone tell me how to exclude text from `<script>` tag within the HTML DOM when calling `element.getText()`?This happens only in Safari (latest browser and driver version)

May 8, 2018 in Selenium by Martin
• 4,340 points
450 views

1 answer to this question.

0 votes

As you have already identified the WebElement as element next you can take help of executeScript() method to extract the text Return to homepage as follows :

String myText = ((JavascriptExecutor)driver).executeScript("return arguments[0].firstChild.textContent;", element);
answered May 8, 2018 by Samarpit
• 5,890 points

Related Questions In Selenium

0 votes
1 answer

How to get the text from all the cells of an HTML table?

This worked for me. Check this out import ...READ MORE

answered May 10, 2019 in Selenium by Surya
• 970 points
340 views
0 votes
0 answers

Getting different texts from the HTML DOM through Selenium and Python or vb.net

Apologies in advance had to hide info ...READ MORE

May 4, 2020 in Selenium by Rock
• 120 points
336 views
0 votes
1 answer

Which exception is raised when an element is not found in an HTML DOM using XPath

Hey, A stale element reference exception is thrown in one of two cases If ...READ MORE

answered Jul 30, 2020 in Selenium by Sirajul
• 59,050 points
1,719 views
0 votes
1 answer

Using gettext() method for the specific element using Selenium WebDriver

Mistake is that u r printing the ...READ MORE

answered Apr 10, 2018 in Selenium by Vardy
• 2,360 points
25,317 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
5,102 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,010 points
5,833 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
3,599 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2, 2020 in Selenium by Sri
• 3,010 points
4,339 views
0 votes
1 answer

Getting different texts from the HTML DOM through Selenium and Python

Try Below code: something = elem.find_element_by_xpath('./td[5]') text1 = driver.execute_script('return ...READ MORE

answered May 14, 2018 in Selenium by Samarpit
• 5,890 points
1,673 views
0 votes
1 answer

Counting the list within HTML structure using Selenium Webdriver

Try Below code: This should work driver.findElements(By.xpath(" ...READ MORE

answered Jul 3, 2018 in Selenium by Samarpit
• 5,890 points
3,516 views