0

I am trying to select a value from a dropdown menu. I tried a lot of solutions found here but nothing work, sometimes I have the error can't scroll to view.

Code trials:

import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains

browser = webdriver.Chrome()  # Optional argument, if not specified will search path.
browser.get('https://dzairannonces.com/posts/create');
mySelectElement = browser.find_element_by_id('parentId')
dropDownMenu = Select(mySelectElement)

I want to select a value from the dropdown form and another value from the second dropdown form that appear when we select the first one

I tried this code too and doesn't work

import unittest
from selenium import webdriver
from selenium.webdriver.support.select import Select
import time


class Drpdowm(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Chrome()

    def test_drpdown(self):
        driver = self.driver
        driver.maximize_window()
        driver.get('https://dzairannonces.com/posts/create')
        time.sleep(10) # Let the user actually see something!
        s1=Select(driver.find_element_by_id('parentId'))

        print(s1.options)


        for opt in s1.options:
            s1.select_by_value(' 315 ')

    def tearDown(self):
        self.driver.quit()
Dharman
  • 30,962
  • 25
  • 85
  • 135
Ubik Bic
  • 43
  • 4

2 Answers2

0

The correct code would be

s1=Select(driver.find_element_by_id('parentId'))
s1.select_by_value('315')

HTML attributes are very strict when it comes to extra spaces or line breaks, you need to provide value exactly as it is

See Select Support chapter for Python WebDriver APIs designed for working with <select> tags

With regards to overall test design a good idea is implementing Page Object Model Design Pattern, it will allow you to split UI and test logic and make your test robust, reliable and easier to refactor. See Page Objects chapter for more information.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
-1

To select an option e.g. Automobiles from a dropdown menu of non <select> tag you can use the following solution:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument('start-maximized')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    driver.get('https://dzairannonces.com/posts/create')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.select2-selection__rendered#select2-parentId-container"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='select2-results']/ul//li[@class='select2-results__option' and contains(., 'Automobiles')]"))).click()
    
  • Browser Snapshot:

nonSelect

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352