-
Notifications
You must be signed in to change notification settings - Fork 4
/
amazon_sel.py
52 lines (37 loc) · 1.64 KB
/
amazon_sel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
class Amazon:
def __init__(self):
self.item = None
self.base_url = 'https://www.amazon.in'
self.browser = webdriver.Chrome()
def search_text(self, item):
'''
Given an item name, selenium opens amazon home page and searches for the product.
Returns the html content of product page. This is used by beautiful soup.
'''
self.item = item
self.browser.get(self.base_url)
search_bar = 'input#twotabsearchtextbox'
self.browser.find_element(By.CSS_SELECTOR, search_bar).send_keys(
self.item + Keys.RETURN)
page_src = self.browser.page_source
return page_src
def next_page(self):
# if there is next page, click on the next page and return page content
try:
element = WebDriverWait(self.browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[text() = 'Next']")))
element.click()
# just waiting page to be loaded !!. No clicking
element = WebDriverWait(self.browser, 10).until(
EC.element_to_be_clickable((By.XPATH, "//a[text() = 'Next']")))
return self.browser.page_source
except:
print('All Pages Traversed !!! ')
# close the browser instance
self.browser.quit()
return -1