我正在创建一个类,当我使用productURLs方法不执行时,该类可以测试我管理的一些站点。除一种特定的方法外,其他所有方法都可以正常工作,我无法弄清原因。不确定我做错了什么,将不胜感激。
仅供参考,我确实尝试研究,但仍然无法弄清楚。
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()
当代码进入producturls方法时,什么都没有发生,只是Chrome窗口在主页上保持打开状态,并且不执行搜索并返回url。
答案 0 :(得分:1)
我在下面添加了工作代码,并在此处列出了我将要更改/将要更改的内容:
希望这会有所帮助!
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)