将数据写入表格后,我会弹出菜单。
我尝试从中选择一些并继续使用硒,但是所有变体都在div
标签中
<div class="menu menu_theme_islands menu_size_m sg-datalist menu__control i-bem sg-datalist_js_inited menu_js_inited menu__control_js_inited sg-datalist_focused menu_focused menu_hovered" data-bem="{"menu":{},"sg-datalist":{}}" role="menu" tabindex="0" aria-activedescendant="uniq155307974964633">
<div class="menu__item menu__item_theme_islands i-bem i-bem" data-bem="{"menu__item":{"val":30863}}" role="menuitem" id="uniq155307974964651" aria-disabled="">Магазин детской одежды</div>
<div class="menu__item menu__item_theme_islands i-bem i-bem" data-bem="{"menu__item":{"val":30859}}" role="menuitem" id="uniq155307974964652" aria-disabled="">Детский магазин</div>
</div>
我正在尝试
company_id = driver.find_element_by_xpath("//input[@placeholder='Начните печатать и выберите из списка']")
company_id.send_keys("Магазин детской одежды")
time.sleep(1)
driver.select.select_by_visible_text("Магазин детской одежды")
但是它返回了一个
AttributeError: 'WebDriver' object has no attribute 'select_by_visible_text'
如何从弹出窗口中选择smth?
答案 0 :(得分:1)
from selenium.webdriver.support.select import Select
Select select=Select(driver.find_element_by_css_selector("div.menu__control_js_inited"))
select.select_by_index(1)
**选项2:**如果上述方法不起作用,请尝试这种方式。
driver.find_element_by_css_selector("div.menu__control_js_inited").click()
WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.XPATH, "(//div[@class='menu__item menu__item_theme_islands i-bem i-bem'])[1]"))).click()
选项3:
element=driver.find_element_by_css_selector("div.menu__control_js_inited")
ActionChains(driver).move_to_element(element).perform()
WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.XPATH, "(//div[@class='menu__item menu__item_theme_islands i-bem i-bem'])[1]"))).click()
请确保您需要进行以下导入。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains
答案 1 :(得分:1)
此错误消息...
AttributeError: 'WebDriver' object has no attribute 'select_by_visible_text'
...表示您尝试通过不支持的 WebDriver 实例使用select_by_visible_text()
。
要单击文本为Магазиндетскойодежды的项目,因为所需元素是<div>
元素,您可以使用以下解决方案:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='menu menu_theme_islands menu_size_m sg-datalist menu__control i-bem sg-datalist_js_inited menu_js_inited menu__control_js_inited sg-datalist_focused menu_focused menu_hovered' and starts-with(@aria-activedescendant, 'uniq')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='menu__item menu__item_theme_islands i-bem i-bem' and text()='Магазин детской одежды']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
答案 2 :(得分:0)
不确定为什么没有得到:
AttributeError: 'WebDriver' object has no attribute 'select'
这不是硒驱动程序的本机属性。
您需要导入Select Class
from selenium.webdriver.support.ui import Select
然后代码应类似于:
company_id = driver.find_element_by_xpath("//input[@placeholder='Начните печатать и выберите из списка']")
company_id.send_keys("Магазин детской одежды")
select = Select(yourMenuElement)
el = select.select_by_visible_text("Магазин детской одежды")
print(el.text)
编辑: 在您的html中,没有select标记,以上内容可能无法正常工作。
您可以尝试以下方法:
company_id = driver.find_element_by_xpath("//input[@placeholder='Начните печатать и выберите из списка']")
input_text = "Магазин детской одежды"
company_id.send_keys(input_text)
menu = driver.find_elements_by_css_selector('[role="menuitem"]')
select_el = None
for el in menu:
if el.text == input_text:
select_el = el
break
if select_el is not None:
"Do stuff"
else:
print("Unable to find element with tex: {}".format(input_text))