0

I am trying to scrape cart items and get their respective price of a shopping website.

So far this is my code but getting an error. I think I am not getting the proper XPath

from  selenium import webdriver
chrome_path =r"C:\Users\aq4'july\Desktop\chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://www.myntra.com/")

#User Login
driver.find_element_by_xpath("""//*[@id="desktop-header-cnt"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/a[2]""")

The error I am getting is

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    driver.find_element_by_xpath("""//*[@id="desktop-header-cnt"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/a[2]""").click()
  File "C:\Users\aq4'july\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\aq4'july\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webelement.py", line 501, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\aq4'july\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
    self.error_handler.check_response(response)
  File "C:\Users\aq4'july\AppData\Roaming\Python\Python36\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64)

Please feel free to Use username and Password (sarwarhayatt@outlook.com, sunset@paris ) respectively for test purpose.

Note: I am using python 3.6 and please highlight I missed anything while asking the question.

  • The expression looks good and should match the element. What error are you getting? Have you tried waiting with an [Explicit Wait](http://selenium-python.readthedocs.io/waits.html#explicit-waits)? – alecxe Dec 24 '17 at 05:20
  • No..Can you please try extending this code also further I don't know how to list cart items – Sarwar Hayatt Dec 24 '17 at 05:26
  • always put full error message (Traceback) in question (as text, not screenshot). There are other useful informations. – furas Dec 24 '17 at 06:06
  • I don't see cart items on page `https://www.myntra.com` but only on `https://www.myntra.com/checkout/cart`. Maybe create minimal working example which we could run. – furas Dec 24 '17 at 06:17
  • @furas Done.Added couple of items to cart. – Sarwar Hayatt Dec 24 '17 at 06:26
  • error shows `"Message: element not visible"` because `Selenium` can't click element if it is not displayed on screen. You have to `click()` icon which shows menu. – furas Dec 24 '17 at 06:39
  • or load directly `https://www.myntra.com/checkout/cart` using `driver.get()` to get pages with items in cart. – furas Dec 24 '17 at 06:41
  • BTW: your xpath tries to click `"PhonePe"` or `"Cashback"` in menu – furas Dec 24 '17 at 06:46
  • @furas that's cool ........but if you are not already logged in nothing will show in the cart. So we have to log in first and logging option come with a hovering button. – Sarwar Hayatt Dec 24 '17 at 06:51
  • I thought you already logged in so you need only get page with cart. After you logged in you can use `https://www.myntra.com/checkout/cart` directly – furas Dec 24 '17 at 06:53

2 Answers2

1

After you logged in you can go to cart directly

driver.get('https://www.myntra.com/checkout/cart')

or you can click "bag" button

item = driver.find_element_by_css_selector('.desktop-cart')
item.click()

Full code tested with Firefox()

EDIT: sometimes it has problem to login but it seems sleep() resolves this problem.

from selenium import webdriver
import time

LOGIN = 'xxx@xxx.com' 
PASSWORD = 'PaSwOrD'

# --- start ---

chrome_path =r"C:\Users\aq4'july\Desktop\chromedriver.exe"
driver = webdriver.Chrome()

#driver = webdriver.Firefox()

# resize window so all elements are visible 
# and the is no problem to click them 
driver.maximize_window()
#driver.set_window_size(1920, 1080)
#driver.execute_script("window.resizeTo(1920,1080)") # doesn't work for me

# --- main page ---

#driver.get("https://www.myntra.com/")

# --- login ---

driver.get('https://www.myntra.com/login?referer=https://www.myntra.com/')

time.sleep(1)

item = driver.find_element_by_css_selector('.login-user-input-email')
item.send_keys(LOGIN)

item = driver.find_element_by_css_selector('.login-user-input-password')
item.send_keys(PASSWORD)

item = driver.find_element_by_css_selector('.login-login-button')
item.click()

time.sleep(1)

# --- main page ---

#driver.get("https://www.myntra.com/")

# --- cart ---

item = driver.find_element_by_css_selector('.desktop-cart')
item.click()

# or

#driver.get('https://www.myntra.com/checkout/cart')

Resizing: How do I resize the window in Chrome and Firefox when testing with Selenium?

Scrolling: How can I scroll a web page using selenium webdriver in python?

furas
  • 134,197
  • 12
  • 106
  • 148
  • just wanna ask you..Does same code can run on the chrome too. If we change `driver = webdriver.Chrome` – Sarwar Hayatt Dec 24 '17 at 07:05
  • it should work on Chrome too - I don't have driver for Chrome in Selenium. – furas Dec 24 '17 at 07:06
  • I installed new Chrome driver and it works (on Linux Mint) but on my computer it opens Chrome with narrow window so `bag` icon is invisible so `click` doesn't work. Code should scroll window or resize window. But using `driver.get()` I can get page with cart – furas Dec 24 '17 at 07:14
  • can you add that code which resizes window in your answer? – Sarwar Hayatt Dec 24 '17 at 07:20
  • Now I'm searching solution for resizing :) – furas Dec 24 '17 at 07:22
1

Bro if you want to login just go to login page and do stuff.... do count double quotes" and escape sequences

from  selenium import webdriver
import time
chrome_path ="chromedriver.exe"
driver = webdriver.Chrome()
driver.get("https://www.myntra.com/login")

time.sleep(2)
driver.find_element_by_xpath("//*[@id=\"mountRoot\"]/div/div/div/form/fieldset[1]/div[1]/input").send_keys("sarwarhayatt@outlook.com")

driver.find_element_by_xpath("//*[@id=\"mountRoot\"]/div/div/div/form/fieldset[1]/div[2]/input").send_keys("sunset@paris")
driver.find_element_by_xpath("//*[@id=\"mountRoot\"]/div/div/div/form/fieldset[2]/button").click()

time.sleep(2)

driver.find_element_by_xpath("//*[@id=\"desktop-header-cnt\"]/div[2]/div[2]/a").click()
time.sleep(2)
print (driver.find_element_by_xpath("//*[@id=\"prod-item-1514127198\"]/div[2]/div[4]/div/div").text)

time.sleep(10)
driver.quit()

your error is right here: 3 double quotes and "desktop-header-cnt" double quotes here is breaking the complete string driver.find_element_by_xpath("""//*[@id="desktop-header-cnt"]/div[2]/div[2]/div/div[2]/div[2]/div[2]/a[2]""")

zxcV32
  • 74
  • 1
  • 4