Petr Petrov
Petr Petrov

Reputation: 4452

AttributeError: 'WebDriver' object has no attribute 'select_by_visible_text' while using select_by_visible_text() method on elements through Selenium

After writing data to form I get the pop-up menu.

I try to select some from it and continue with selenium, but all variants are in div tag

<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="{&quot;menu&quot;:{},&quot;sg-datalist&quot;:{}}" role="menu" tabindex="0" aria-activedescendant="uniq155307974964633">
    <div class="menu__item menu__item_theme_islands i-bem i-bem" data-bem="{&quot;menu__item&quot;:{&quot;val&quot;:30863}}" role="menuitem" id="uniq155307974964651" aria-disabled="">Магазин детской одежды</div>
    <div class="menu__item menu__item_theme_islands i-bem i-bem" data-bem="{&quot;menu__item&quot;:{&quot;val&quot;:30859}}" role="menuitem" id="uniq155307974964652" aria-disabled="">Детский магазин</div>
</div>

I'm trying

company_id = driver.find_element_by_xpath("//input[@placeholder='Начните печатать и выберите из списка']")
company_id.send_keys("Магазин детской одежды")
time.sleep(1)
driver.select.select_by_visible_text("Магазин детской одежды")

But it's return an

AttributeError: 'WebDriver' object has no attribute 'select_by_visible_text'

How can I select smth from the pop-up?

Upvotes: 1

Views: 12411

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193208

This error message...

AttributeError: 'WebDriver' object has no attribute 'select_by_visible_text'

...implies that you have tried to use select_by_visible_text() through a WebDriver instance which is not supported.

Solution

To click on the item with text as Магазин детской одежды as the desired elements are <div> elements you can use the following solution:

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()

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 1

KunduK
KunduK

Reputation: 33384

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)

**Option 2 :**If above doesn't work try this way.

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()

Options 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()

Please make sure you need to have following imports.

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

Upvotes: 3

Nic Laforge
Nic Laforge

Reputation: 1876

Not sure why you did not get:

AttributeError: 'WebDriver' object has no attribute 'select'

As this is not a native attribute of the selenium driver.

You need to import the Select Class

from selenium.webdriver.support.ui import Select

Then code should look like:

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)

Edit: From your html, there is no select tag and the above might not work.

You could try this:

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))

Upvotes: 0

Related Questions