0

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.

1
  • Property getters are meant to return something. What do you expect the statement new_urls.getpage to do? Commented Jan 23, 2019 at 5:02

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.