-2

I want to get the text "Cleaning General: Audit Based Update" in the HTML below. It should have the xpath:

//*[@id="context_bar"]/table/tbody/tr/td[5]/div

How do I convert the following Python ElementTree code to Selenium?

browser.find_element_by_xpath('//*[@id="context_bar"]/table/tbody/tr/td[5]/div/@title').text()

with the following HTML:

image

hakukou
  • 111
  • 1
  • 10
  • 1) We can't reproduce this because we don't have the HTML. Please post the URL. 2) What error/issue do you get? When you try debugging your XPATH by successively adding parts to it, where does it stop working? – smci Sep 12 '19 at 00:17
  • hi, 1) Internal site 2) I Just want to print text. – hakukou Sep 12 '19 at 00:35
  • 1) Ok but we can't reproduce it. 2) Doesn't matter. To debug the xpath, the only sane method I know is start with just `'//*[@id="context_bar"]/table/tbody`, check it works, then add xpath components one-at-a-time until you find where the problem is. Tell us where that is. If you don't debug it and describe the problem better, then we can't help you. – smci Sep 12 '19 at 00:37
  • hi,this code can you convert to selenium ```title = html.etree.HTML(content).xpath('//*[@id="context_bar"]/table/tbody/tr/td[5]/div/@title')[0]``` – hakukou Sep 12 '19 at 00:47
  • Oh, you're asking us to convert Python ElementTree code to Selenium. I don't know Selenium but it should be easy, see the Selenium doc. – smci Sep 12 '19 at 00:56

1 Answers1

2

The method you're calling is find_element_by_xpath so it will return an element. if you want to get an attribute of an element then you'll need to call elem.get_attribute(...)

div = browser.find_element_by_xpath('//*[@id="context_bar"]/table/tbody/tr/td[5]/div')
title = div.get_attribute('title')