[英]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.