boston_019
boston_019

Reputation: 7

Need Selenium to return the class title content of given HTML

Using Selenium to perform some webscraping. Have it log in to a site, where an HTML table of data is returned with five values at a time. I'm going to have Selenium scrape a particular bit of data off the table, write to a file, click next, and repeat with the next five.

New automation script. I've a myriad of variations of get_attribute, find_elements_by_class_name, etc. Example:

pnum = prtnames.get_attribute("title")
for x in prtnames:
    print('pnum')

Here's the HTML from one of the returned values:

<div class="text-container prtname"><span class="PrtName" title="P011">P011</span></div>

I need to get that "P011" value. Obviously Selenium doesn't have "find_elements_by_title", and there is no HTML id for the value. The Xpath for that line of HTML is:

//*[@id="printerConnectTable"]/tbody/tr[5]/td/table/tbody/tr[1]/td[2]/div/span

But I don't see a reference to "title" or "P011" in that Xpath.

    pnum = prtnames.get_attribute("title")
AttributeError: 'list' object has no attribute 'get_attribute'

It's like get_attribute doesn't exist, but there is some (albeit not much) documentation on it.

Fundamentally I'd like to grab that "P011" value and print to console, then I know Selenium is working with the right data.

P.S. I'm self-taught with all of this, I'm automating a sysadmin task.

Upvotes: 0

Views: 89

Answers (1)

LuckyZakary
LuckyZakary

Reputation: 1191

I think the problem is that prtnames is a list of element, not a specific element. You can use a list comprehension if you want a list of the attributes of titles for the list of prtnames.

pnums = [x.get_attribute('title') for x in prtnames]

Upvotes: 1

Related Questions