Reputation: 253
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
Reputation: 2755
I've added the working code below and have listed the things I changed/would change here:
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