Reputation: 38
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 ([email protected], sunset@paris ) respectively for test purpose.
Note: I am using python 3.6 and please highlight I missed anything while asking the question.
Upvotes: 0
Views: 1014
Reputation: 114
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("[email protected]")
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]""")
Upvotes: 1
Reputation: 142641
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 = '[email protected]'
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?
Upvotes: 1