How python automates the test selenium operation drop-down list
This article mainly introduces python how to automate the test selenium operation drop-down list, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
To deal with the drop-down list, you need to use the utility class Select in selenium. The common methods are as follows:
Sample website: http://sahitest.com/demo
Example scenario: open the Sahi Tests page
(1) Click the "Select Test" page and click the first drop-down list on the page.
Sample script:
From selenium import webdriverfrom time import sleepfrom selenium.webdriver.support.select import Selectclass TestSelected (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get ("https://sahitest.com/demo/") def test_selected (self): # dot" Select Test "link self.driver.find_element_by_xpath (" / html/body/table/tbody/tr/td [1] / A [4] "). Click () # Point the first drop-down box se=self.driver.find_element_by_id (" s1Id ") # Select the drop-down box option select=Select (se) # print the drop-down box option for options in select.options: print (options.text)
Running result:
(2) operate multiple selection list
Sample script:
From selenium import webdriverfrom time import sleepfrom selenium.webdriver.support.select import Selectclass TestSelected (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get ("https://sahitest.com/demo/") def test_multiselected (self): # dot" Select Test "link self.driver.find_element_by_xpath (" / html/body/table/tbody/tr/td [1] / a [4] " ). Click () # list checkbox mulsel = self.driver.find_element_by_id ("s4Id") select2 = Select (mulsel) # Select all options in the list for i in range (6): select2.select_by_index (I) # reverse selection based on index value # select2.deselect_by_index (I) sleep (1) sleep (2) # invert all select2.deselect_all () self.driver.quit () Thank you for reading this article carefully I hope the article "python how to automate the test selenium operation drop-down list" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!