简体   繁体   English

如何通过Selenium Webdriver Python选择“排序依据”元素

[英]How can I select the “Sort By” element through Selenium Webdriver Python

I was trying to automate one task using Selenium Webdriver with Python 2.7.The task goes as follows: 1. Open " https://www.flipkart.com ". 我试图使用带有Python 2.7的Selenium Webdriver自动化一项任务。该任务如下:1.打开“ https://www.flipkart.com ”。 2. Search for 'laptop'. 2.搜索“笔记本电脑”。 3. Hit search button. 3.点击搜索按钮。 4. Whatever is the answer of the search query, sort them "By Popularity" using the sort button. 4.无论搜索查询的答案是什么,请使用“排序”按钮将它们“按受欢迎程度”排序。

Code

from selenium import webdriver

url="https://www.flipkart.com"
xpaths={'submitButton' :   "//button[@type='submit']",
    'searchBox' : "//input[@type='text']"}

driver=webdriver.Chrome()
driver.maxmimize_window()

driver.get(url)

#to search for laptops
driver.find_element_by_xpath(xpaths['searchBox']).clear()
driver.find_element_by_xpath(xpaths['searchBox']).send_keys('laptop')
driver.find_element_by_xpath(xpaths['submitButton']).click()

#to sort them by popularity
driver.find_element_by_xpath("////*[@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]").click()

The last statement throws an error: 最后一条语句引发错误:

raise exception_class(message, screen, stacktrace)
InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression ////*[@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2] because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '////*[@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]' is not a valid XPath expression.
  (Session info: chrome=53.0.2785.143)
  (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)

Given I copied the xpath for that particular element "Sort By - Popularity" using the Chrome developer tools(ctrl+shift+I). 给定我使用Chrome开发人员工具(ctrl + shift + I)复制了该特定元素“按流行程度排序”的xpath。

And also, it highlights the same element when I try to search for that xpath in the console window of developer tools. 而且,当我尝试在开发人员工具的控制台窗口中搜索该xpath时,它会突出显示相同的元素。

What's wrong with the xpath? xpath怎么了? Help! 救命!

As far as syntax error goes replace //// with // in your xpath 就语法错误而言,在xpath中将////替换为//

driver.find_element_by_xpath("//*@id='container']/div/div[2]/div[2]/div/div[2]/div[2]/div/section/ul/li[2]").click()

This will resolve your syntax error and will do the required job Though In my opinion a better XPATH would be driver.find_element_by_xpath("//li[text()='Popularity']").click() 这将解决您的语法错误并完成所需的工作,尽管在我看来,更好的XPATH是driver.find_element_by_xpath("//li[text()='Popularity']").click()

you can use following code. 您可以使用以下代码。

from selenium import webdriver
import time
url="https://www.flipkart.com"
xpaths={'submitButton' :   "//button[@type='submit']",
    'searchBox' : "//input[@type='text']"}

driver=webdriver.Chrome()
#   driver.maxmimize_window()

driver.get(url)

#to search for laptops
driver.find_element_by_xpath(xpaths['searchBox']).clear()
driver.find_element_by_xpath(xpaths['searchBox']).send_keys('laptop')
driver.find_element_by_xpath(xpaths['submitButton']).click()

#to sort them by popularity
time.sleep(5)
driver.find_element_by_xpath("//li[text()='Popularity']").click()

You may try with this css selector too: 您也可以尝试使用此CSS选择器:

#container > div > div:nth-child(2) > div:nth-child(2)  > div > div:nth-child(2)  > div:nth-child(2)  > div > section > ul > li:nth-child(2)

To me, cssSelector is better than xpath for browser compatibility too. 对我来说,cssSelector在浏览器兼容性方面也优于xpath。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当页面上有这么多元素时,我如何 select 页面上的特定元素? Selenium webdriver python; - How can I select a specific element on a page when there is so many of that element? Selenium webdriver python; 我如何等待元素可见,然后在Python Selenium Webdriver中单击? - How Can I wait for an Element will be visible then be clicked in Python Selenium Webdriver? 如何通过Selenium / Python选择没有ID的Web元素 - How do I select a web element with no ID through Selenium/Python 如何通过Selenium WebDriver和Python找到用户名和密码元素 - How to locate the username and password element through Selenium WebDriver and Python 如何随机 select 并使用 selenium webdriver(Python)单击元素? - How to randomly select and click on element with selenium webdriver (Python)? 如何在 Selenium WebDriver 的下拉 div 菜单中 select 一个元素? (Python) - How do I select an element in dropdown div menu in Selenium WebDriver? (Python) 如何在python中使用selenium选择具有特定`title`的元素 - How can I select an element with a specific `title` using selenium in python 我如何在不被发现的情况下抓取网站并通过Python使用Selenium Webdriver绕过reCAPTCHA? - How can I scrape a website without getting detected and bypassing reCAPTCHA using selenium webdriver through Python? 如何使用 selenium webdriver select 元素样式 - How to select an element style with selenium webdriver 找到Python Selenium Webdriver元素,但无法单击它 - Python Selenium Webdriver element found but can not click on it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM