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,320 points
1,286 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,910 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
1,956 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
899 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,230 points
10,287 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,500 points
12,615 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,190 points
8,571 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,910 points
6,629 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 3, 2020 in Selenium by Sri
• 3,190 points
7,517 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,910 points
2,777 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,910 points
5,404 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP