Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python selenium xpath error [object XrayWrapper [object Attr]]

I'm writing a script that collects all auctions from auction based service. I want to get all templates with selenium xpath by I'm occuring an error all the time. I have managed to download the auctions descriptions from page with xpath, but I cant extract their hrefs. I`m using both xpath finder (for firefox) and xpath helper (for chrome), and it displays me my links. But I cant revere to them.

Bellow I'm presenting my script, my console log and link that I want to extract.

    SCRIPT (part of it)
while next_page_available:
            wait = WebDriverWait(driver, 10)
            element = wait.until(EC.presence_of_element_located((By.XPATH, "//li[contains(@class, 'nav-btn next-site')]")))
            element = driver.find_element_by_xpath("//li[contains(@class, 'nav-btn next-site')]")
            if element.is_displayed():
                #THIS IS THE PART WHERE IT CRASHES
links = driver.find_elements_by_xpath('//tbody/tr/td[3]/a[1]/@href')

                print "LINK: ", links
                for link in links:
                    value = link.get_attribute("content")
                    items_names.append(value)
                    count += 1
                    print "hi"
                    print count, " val: ", value

                element.click()

            else:
                print "No more pages with auctions"
                next_page_available = False

Console log:

#########My console log: 
InvalidSelectorError: The result of the xpath expression '//tbody/tr/td[3]/a[1]/@href' is: [object XrayWrapper [object Attr]]. It should be an element.

And auction link that i want to get to:

<a href="http://ms.allegro.pl/template/edit/563656/" title="Buty robocze, WODOODPORNE TW400 Panoply roz.43" class=" xh-highlight">Buty robocze, WODOODPORNE TW400 Panoply roz.43</a>

Thanks for your help, Best wishes

like image 902
sebb Avatar asked Mar 11 '15 10:03

sebb


2 Answers

SOLUTION:

I've gotten to a documentation where I found, that I can pull out a attribute.

All you have to do is take out a attribute from the body of your requested item. In my particular case I was looking for href (example bellow). If you were looking for lets say title of a link, just type get_attribute("type") all will be running smoothly.

links = driver.find_elements_by_xpath("//a[contains(@href,'http://ms.allegro.pl/template/edit/')]")
                for link in links:
                    value = link.get_attribute("href")

Thanks for your reply. Best wishes

like image 102
sebb Avatar answered Sep 30 '22 03:09

sebb


You are using this XPath expression:

//tbody/tr/td[3]/a[1]/@href

This is an expression that would return a series of attributes. This is perfectly fine as XPath goes. However, when you use XPath through Selenium there is a restriction as to what XPath expressions can return: XPath expressions used with the find_element(s)... methods must return elements, and nothing else. If you remove @href, you'll get an element. If what you wanted are those elements that have an href attribute, you could replace a[1] with a[position() = 1 and @href].

like image 25
Louis Avatar answered Sep 30 '22 03:09

Louis