'I am able to extract phone price from amazon and flipkart but not able to compare them with selenium python
import time
from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
ops = ChromeOptions() # to disable notifications
ops.add_argument("--disable-notifications")
driver = Chrome(ChromeDriverManager().install(), options=ops) # install chrome driver manager
driver.maximize_window() # Maximising the tab
driver.get("https://www.amazon.in/") # entering the browser url
driver.implicitly_wait(20)
driver.find_element_by_id("twotabsearchtextbox").send_keys('iPhone-XR(64GB)-Yellow') # search for iphone xr phone
driver.find_element_by_css_selector("input[type='submit']").click() # clicking the search results
link = driver.find_element_by_class_name("a-price-whole") # extracting the price value and storing it in a variable
# print(link.text) #printing the stored variable in text
k = link.text
m = []
for pric in k:
if pric.isdigit() == True:
m.append(pric)
amazonprice = ''.join(m)
amazonvalue = int(amazonprice)
print(amazonvalue)
print(type(amazonvalue))
driver.get("https://www.flipkart.com/") # browse fllipakrt website
driver.find_element_by_xpath('/html/body/div[2]/div/div/button').click() # closing the login window pop
driver.find_element_by_name('q').send_keys('iPhone-XR(64GB)-Yellow') # search for iphone in flipkart
driver.find_element_by_xpath("//button[@class='L0Z3Pu']").click()
options = ChromeOptions()
time.sleep(2)
driver.find_element_by_xpath("//div[contains(text(),'APPLE iPhone XR (Yellow, 128 GB)')]").click()
p = driver.window_handles
print("Parent win : ", driver.current_window_handle)
# get first child window
for x in p:
driver.switch_to.window(x)
print("Child window title: " + driver.title)
time.sleep(2)
print("Child win : ", driver.current_window_handle)
val = driver.find_element_by_css_selector("._30jeq3._16Jk6d").text
y = []
for x in val:
if x.isdigit() == True:
y.append(x)
flipkartprice = ''.join(y)
flipkartvalue = int(flipkartprice)
print(flipkartvalue)
print(type(flipkartvalue))
if flipkartvalue > amazonvalue:
print(flipkartvalue)
elif flipkartvalue == amazonvalue:
print("both are equal")
else:
print(amazonvalue)
I am trying to extract phone price from amazon and flipkart website. And comparing those value. In the above code it will able to extract the values from both flipkart and amazon site. Both i am not able to compare the values. Kinldy help
Solution 1:[1]
What do you mean by not able to compare the values?
In your condition, you wrote:
if flipkartvalue > amazonvalue:
print(flipkartvalue)
elif flipkartvalue == amazonvalue:
print("both are equal")
else:
print(amazonvalue)
The price from amazon = 54999 and flipkart = 44999
The console prints 54999 (else block). Everything seems working fine.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kongvungsovanreach |
