简体   繁体   中英

Python and Selenium xpath text

How can I get the text of the following xpath:

.//*[@id='answer']/section/table/tbody/tr[45]/td[4]/text()[1]

Here is the HTML segment I'm trying to get:

...
<td>first part<span id="divider">/</span>second part of column</td>
...

I have something like:

url = ".//*[@id='answer']/section/table/tbody/tr[45]/td[4]/text()[1]"
value = browser.find_element_by_xpath(url)

then I tried:

print value.text
print value.get_attribute("innerHTML")

But I have an error with both

UPDATE: Here are the errors I have
With text :

Traceback (most recent call last):
File "select_element.py", line 195, in <module>
print value.text
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 73, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: Argument 1 of Window.getComputedStyle does not implement interface Element.

With get_attribute :

Traceback (most recent call last):
  File "select_element.py", line 195, in <module>
    print value.get_attribute("innerHTML")
  File "/Library/Python/2.7/site-
packages/selenium/webdriver/remote/webelement.py", line 139, in get_attribute
self, name)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 491, in execute_script
'args': converted_args})['value']
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: a.getAttributeNode is not a function

Thanks, Arty

The xpath which you have used for url is incorrect:

Xpath Used:

url = ".//*[@id='answer']/section/table/tbody/tr[45]/td[4]/text()[1]"

It should be:

url = ".//*[@id='answer']/section/table/tbody/tr[45]/td[4]"

Instead of xpath you should use ID with BeautifulSoup like this:

for row in soup.find_all('td',attrs={"id" : "divider"}):
        print row.text

This wil solve your problem. :) Happy coding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM