简体   繁体   English

单击不是 SELECT 元素的下拉菜单的第一个元素-Python selenium

[英]Clicking first element of drop down menu that is not a SELECT element -Python selenium

I have a drop down menu that I haven't been able to figure out how to click the first element for.我有一个下拉菜单,我无法弄清楚如何单击第一个元素。 It is not a SELECT element, so I have been clicking the drop down, waiting for elements to be visible, and then try selecting the first option.它不是SELECT元素,所以我一直在单击下拉菜单,等待元素可见,然后尝试选择第一个选项。 That has not been working, and I'm not sure what I'm missing.那一直没有用,我不确定我错过了什么。

driver.get("https://bi.prozorro.org/sense/app/2595af2b-985f-4771-aa36-2133e1f89df0/sheet/48781d08-1fce-489b-af05-34c253e95ec2/state/analysis#view/pEh")

#click for menu to appear
tenderercode=WebDriverWait(driver, 25).until(EC.presence_of_element_located((By.XPATH,"//span[@class='title ng-binding']")))
tenderercode.click()
sleep(3)

#entering text in menu
element = driver.find_element_by_xpath("//input[@placeholder='Search in listbox']")
element.click()
element.send_keys("test")

#click on first element  
first=WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CSS_SELECTOR,
                                                                "body.qv-client.qv-sheet-enabled.qv-view-sheet:nth-child(2) div.ng-scope.ng-isolate-scope div.lui-popover-container.ng-scope div.qv-listbox-popover.ng-scope.lui-popover div.lui-nopad.lui-popover__body.ng-scope div.listbox-wrapper.ng-scope div.qv-object-wrapper.ng-isolate-scope article.qv-object.qvt-visualization.qv-layout-xsmall.qv-object-listbox.qv-selections-active div.qv-inner-object div.qv-object-content-container:nth-child(4) div.qv-object-content.ng-isolate-scope div.qv-listbox-container.ng-scope.qv-listbox-interactive div.qv-listbox-wrapper.searchActive div.qv-listbox-scroll-area.qv-animate.ng-isolate-scope.qv-listbox-virtual-scroll-enabled div.scroll-content:nth-child(1) ul.qv-listbox.ng-scope > li.qv-listbox-item.ng-scope.serverOptional:nth-child(1)")))

first.click()

Error: selenium.common.exceptions.TimeoutException错误: selenium.common.exceptions.TimeoutException

The timeout error makes me think my css selector is incorrect, but I've tried the xpath too, and that hasn't fared much better.超时错误让我觉得我的 css 选择器不正确,但我也尝试过 xpath,但效果并没有好多少。 A little bit at my wits end, so any ideas are much appreciated.有点不知所措,所以非常感谢任何想法。

Change:改变:

first=WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "your locator")))
first.click()

to

WebDriverWait(driver, 40).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "your locator")))
first = driver.find_element_by_css_selector("your locator")
first.click()

And fix locator.并修复定位器。 It is not the main issue, but it's also the problem.这不是主要问题,但也是问题所在。

Update.更新。 I also find one more locator.我还找到了另一个定位器。 To find the first item use the locator use:要查找第一个项目,请使用定位器:

driver.find_element_by_css_selector(".qv-listbox-item.serverOptional:nth-of-type(1)>.qv-listbox-text>span")

To click use:点击使用:

driver.find_element_by_css_selector(".qv-listbox-item.serverOptional:nth-of-type(1)>.qv-listbox-text>span>span")

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

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