SoFunction
Updated on 2024-10-26

python+selenium manipulate dropdown box

Take this site as an example:https:///pins/demo-show?id=5926

There are multiple drop-down boxes available for testing under this page.

Basic scaffolding code:

from  import Select
from selenium import webdriver
import time

driver = ()

('https:///pins/demo-show?id=5926')

# Switch to iframe
driver.switch_to.frame(driver.find_element_by_id('iframe'))

Positioning drop-down boxes (using the first drop-down box as an example)

Select() method

select_elm = Select(driver.find_element_by_class_name('nojs'))

This positioning is not recommended:

select_elm = driver.find_element_by_class_name('nojs').click()

Check the drop-down box and click:

driver.find_element_by_xpath('//option[@value="JP"]').click()

The optional values for the first drop-down box are shown in the figure:

There are three ways to select the value in the drop-down box

1. Selected according to the index

select_elm.select_by_index(2)

conforms to the indexing rules, counts from 0, and automatically selects theCanadaValue.

2. Selection based on value

select_elm.select_by_value('CA')

The value here refers to thevalueattribute instead of a text string.

The results of the run are the same as above.

3. Selection based on visual text

select_elm.select_by_visible_text('Canada')

That is, what is seen is what is chosen.
The results of the run are the same as above.

Up to this point this article onpython+seleniumoperation of the drop-down box of the article is introduced to this, more related python selenium operation of the drop-down box content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!