How to select an option from a dropdown of non select tag?

半腔热情 提交于 2020-01-26 04:33:17

问题


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

回答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:




回答2:


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.



来源:https://stackoverflow.com/questions/57399713/how-to-select-an-option-from-a-dropdown-of-non-select-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!