简体   繁体   English

找不到硒元素下拉菜单python

[英]Selenium element cannot be found drop down menu python

I am using Selenium WebDriver on the eBay website. 我正在eBay网站上使用Selenium WebDriver。 I am trying to change the drop down menu from best match to lowest price + P&P. 我正在尝试将下拉菜单从最佳匹配更改为最低价格+ P&P。 This is my code: 这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
sortdown = browser.find_element(By.XPATH, '//*[@id="DashSortByContainer"]/ul[1]/li/div/a')
sortselect = Select(sortdown)
sortselect.select_by_visible_text('Lowest price + P&P')

I have used XPATH in case the element is dynamic. 如果元素是动态的,我已经使用了XPATH。 And python still says the element cannot be found. 而且python仍然说找不到元素。 Can anyone help? 有人可以帮忙吗? Here is an example link with the drop down menu at the top right of the results: https://www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=harley&_sacat=0 这是一个示例链接,其链接位于结果右上角: https : //www.ebay.co.uk/sch/i.html?_from=R40&_trksid=m570.l1313&_nkw=harley&_sacat=0

The items it displays are not real Select elements. 它显示的项目不是真实的Select元素。 It's just a <ul with bunch of links, hidden by default. 它只是一个<ul带有一堆链接,默认情况下是隐藏的。 So I would suggest something like this: 所以我建议这样的事情:

  1. Select current sort link ( <a ) to open list of other options. 选择当前的排序链接( <a )以打开其他选项的列表。 Easiest to do so is by link text. 最简单的方法是通过链接文本。 Selectors like ul[1]/li/div/ are just confusing and unnecessary. 诸如ul[1]/li/div/类的选择器只是令人困惑且不必要。 Note that depending on what precedes this operation, you may also need this link to show up. 请注意,根据此操作之前的内容,可能还需要显示此链接。
  2. Select new option once it shows up (ie wait for it, then click). 一旦新选项出现,请选择它(即等待它,然后单击)。

Eg: 例如:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(browser, 10)

# click link to display all options
sortdown = browser.find_element_by_link_text('Best Match')
sortdown.click()

# select a new option
lowestprice = wait.until(EC.presence_of_element_located((By.LINK_TEXT, 'Lowest price + P&P')))
lowestprice.click()

The element //*[@id="DashSortByContainer"]/ul[1]/li/div/a is a link, not select. 元素//*[@id="DashSortByContainer"]/ul[1]/li/div/a是链接,而不是select。 That's why you cannot use Select class. 这就是为什么您不能使用Select类的原因。
You need click on the //*[@id="DashSortByContainer"]/ul[1]/li/div/a , then locate you elements with values in the DOM and click one you need. 您需要click //*[@id="DashSortByContainer"]/ul[1]/li/div/a ,然后在DOM中找到具有值的元素,然后单击所需的元素。

You can find similar question here with answer you can use as a reference. 您可以在此处找到类似的问题以及可以用作参考的答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM