我正在尝试刮取购物车物品并获得购物网站的相应价格。
到目前为止,这是我的代码但收到错误。我想我没有得到正确的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]""")
我得到的错误是
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)
请分别使用用户名和密码(sarwarhayatt@outlook.com,sunset @ paris)进行测试。
注意:我正在使用python 3.6,请注意我在提问时错过了任何内容。
答案 0 :(得分:1)
登录后,您可以直接进入购物车
driver.get('https://www.myntra.com/checkout/cart')
或者您可以点击“包”按钮
item = driver.find_element_by_css_selector('.desktop-cart')
item.click()
使用Firefox()
编辑:有时登录有问题但似乎sleep()
可以解决此问题。
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')
调整大小:How do I resize the window in Chrome and Firefox when testing with Selenium?
滚动:How can I scroll a web page using selenium webdriver in python?
答案 1 :(得分:1)
如果您想登录,请转到登录页面 做点什么.... 做双引号计数“和转义序列
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()
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()
你的错误就在这里: 3个双引号和“desktop-header-cnt”双引号在这里打破了完整的字符串 driver.find_element_by_xpath( “” “// * [@ ID =” 桌面头-CNT“] / DIV [2] / DIV [2] / DIV / DIV [2] / DIV [2] / DIV [2] /一个[2] “”“)