0

I have a question related to Selenium in Python:

I want to obtain the text content "D. New Jersey" on a webpage. In addition, the text that I want to get can be different on different pages, but it is always under "COURT:".

The HTML code is:

<div class="span4">
  <strong>COURT:</strong>
  D. New Jersey
</div>

The code I use now is as follows. And it doesn't work.

self.driver.get(address) 
element=driver.findElement("//a[contains(@class,'span4') and contains(div/div/text(),'COURT:')]").gettext() 

I have also tried the following solutions with no luck, and no Selenium exception is being thrown either:

text = self.driver.find_element_by_xpath("//div[strong[text()='COURT:']]").text

and

text = self.driver.find_element_by_xpath("//a[contains(@class,'span4') and contains(div/div/text(),'COURT:')]").text

Is there anyone who knows how to get the text from this code using Selenium?

Thanks

5
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Oct 8, 2019 at 16:52
  • Hi Austen. There is no way that I am fishing any code here lol. I simply wanted to be straight to the point. The code I use is : self.driver.get(address) element=driver.findElement("//a[contains(@class,'span4') and contains(div/div/text(),'COURT:')]").gettext() However it simply doesn't work out. Commented Oct 8, 2019 at 16:56
  • 1
    If you could edit your question to add that code, that would make it much easier for someone to be able to assist you. Commented Oct 8, 2019 at 16:58
  • 2
    Advice adopted. Commented Oct 8, 2019 at 17:00
  • In Python, web elements do not support a gettext() function -- you just need to use text, an attribute. I've added a solution using the text attribute along with a refined XPath to help with your issue. Commented Oct 8, 2019 at 17:04

1 Answer 1

3

For Python, you can get the text as such:

text = self.driver.find_element_by_xpath("//div[strong[text()='COURT:']]").text

This uses an XPath to query on the div element, using its inner strong element to ensure we have selected the correct div. Then, we call Python's webelement.text method to get the div's text.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi Christine. I tried your solution, and added print(text). However, Python doesn't seem to print anything and get the text I wanted...
Can you try printing the WebElement to see if it's been located? element = driver.find_element_by_xpath("//div[strong[text()='COURT:']]"), followed by print(element)
It seems like the element is not getting located correctly. Is there any exception being thrown or error message in the console? Selenium usually throws a NoSuchElement exception if you are trying to find something that does not exist. and that error should display in the console. I've updated my solution to use the original XPath you posted in your question -- let's see if that makes a difference.
No, there is simply nothing prompted on the screen. There's no error message.
Again, using my original code with .text doesn't throw anything either...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.