dcarlo56ave
dcarlo56ave

Reputation: 253

Method in Python Class Not Executing

I am creating a class that will allow me to test a few sites I manage however, when I use the producturls method its not executing. Everything works with the exception of one particular method and I cannot figure out why. Not sure what I am doing wrong any help would be appreciated.

FYI I did try researching but still cannot figure this out.

class SearchCheck:

    def __init__(self, url):
        self.url = url
        self.driver = webdriver.Chrome()

    @property
    def getpage(self):
        self.driver.get(self.url)
        self.driver.implicitly_wait(10)

    @getpage.setter
    def getpage(self, url):
        self.url = url
        self.driver.get(self.url)
        self.driver.implicitly_wait(10)


    def producturls(self):
        search = self.driver.find_element_by_xpath('//*[@id="search-box"]/div[2]/div/div[1]/div/div[1]/input')
        time.sleep(5)
        search.sendkeys('shoes')
        search.sendkeys(Keys.ENTER)
        driver.implicitly_wait(60)
        # Loop through and get links
        for a in self.driver.find_elements_by_xpath('//*[@id="products"]/div[2]/div/div/div/div/div/a'):
            yield a.get_attribute('href')


if __name__ == '__main__':
    start_page = 'https://www.google.com'
    new_urls = RankChecker(start_page)
    new_urls.getpage
    new_urls.producturls()

When the code gets to the producturls method nothing happens just chrome windows stays open on the home page and does not perform the search and return the urls.

Upvotes: 0

Views: 50

Answers (1)

cullzie
cullzie

Reputation: 2755

I've added the working code below and have listed the things I changed/would change here:

  1. Your xpaths were incorrect. You can test them in your browsers developer mode
  2. webdriver send_keys() is the method to call not sendkeys()
  3. You were missing a self on front of driver inside the producturls method
  4. You referenced RankChecker in your main method even though your class is called SearchCheck
  5. You need to loop over the response from producturls since it returns a generator because of the yield keyword
  6. A getter method should return a value. getpage would make more sense as a method since it is doing something instead of accessing a property
  7. Python methods/attributes should be of the form get_page, product_urls etc. I haven't changed these but looking through the python style guide would help improve these things

Hope this helps!

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class SearchCheck:
    def __init__(self, url):
        self.url = url
        self.driver = webdriver.Chrome()

    @property
    def getpage(self):
        self.driver.get(self.url)
        self.driver.implicitly_wait(10)
        return

    @getpage.setter
    def getpage(self, url):
        self.url = url
        self.driver.get(self.url)
        self.driver.implicitly_wait(10)

    def producturls(self):
        search = self.driver.find_element_by_xpath('//input[@title="Search"]')
        time.sleep(5)

        search.send_keys('shoes')
        search.send_keys(Keys.ENTER)
        self.driver.implicitly_wait(60)
        # Loop through and get links
        for a in self.driver.find_elements_by_xpath('//div[@class="srg"]//div[@class="g"]//a'):
            yield a.get_attribute('href')

if __name__ == '__main__':
     start_page = 'https://www.google.com'
     new_urls = SearchCheck(start_page)
     new_urls.getpage
     urls = [url for url in new_urls.producturls()]
     print(urls)

Upvotes: 1

Related Questions