예를 들면 다음과 같습니다.
<select id="fruits01" class="select" name="fruits">
<option value="0">Choose your fruits:</option>
<option value="1">Banana</option>
<option value="2">Mango</option>
</select>
1) 우선 내가 클릭해야 할 것입니다. 나는한다 이:
inputElementFruits = driver.find_element_by_xpath("//select[id='fruits']").click()
2) 말하도다 '망고' 를 보유하고 있는 후 좋은 요소점 수 있습니다.
함께 할 '찾았다' inputElementFruits.send_keys (.) 그러나 작동되지 않았다.
['선택' 클래스] [1] 가 제공하는 편리한 작업할 수 있는 '선택 - >. 옵션 '구문.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Firefox()
driver.get('url')
select = Select(driver.find_element_by_id('fruits01'))
# select by visible text
select.select_by_visible_text('Banana')
# select by value
select.select_by_value('1')
참고:
[1]: https://selenium-python.readthedocs.io/api.html # module-selenium.webdriver.support.select
Ajax 호출 발생) 을 않는다면, 당신은 당신의 함정이거나 채우려면 don& # 39 는 목록 클릭, 실제로 실행할 필요가 없다.
그냥 요소를 찾기 그리곤요 열거하십시오 %hsmb 옵션, 원하는 옵션을 선택하여 적용할 수 있습니다.
예를 들어보겠습니다.
from selenium import webdriver
b = webdriver.Firefox()
b.find_element_by_xpath("//select[@name='element_name']/option[text()='option_text']").click()
더 많이 읽을 수 있습니다. br /> <; https://sqa.stackexchange.com/questions/1355/unable-to-select-an-option-using-seleniums-python-webdriver
우선 선택 후 가져오기할 필요한 클래스를 만드는 데 필요한 인스턴스입니다 선택 클래스. 만든 후 인스턴스입니다 선택 옵션을 선택할 수 있는 방법을 선택할 수 있는 선택 클래스 인스턴스 드롭다운이 나열하십시오 수행할 수 있습니다. 다음은 코드
from selenium.webdriver.support.select import Select
select_fr = Select(driver.find_element_by_id("fruits01"))
select_fr.select_by_index(0)
그런데 우리 내부에 있던 많은 것들이 전화했는데 많이유 드롭다운하십시오 테이블 및 전 간단한 작업을 수행할 수 없는 선택. 아래 솔루션이므로 만 했다. Here I am 강조표시하기 화살표 눌러서 원하는 값을 가져오는 때까지 드롭다운하십시오 요소 및 -
#identify the drop down element
elem = browser.find_element_by_name(objectVal)
for option in elem.find_elements_by_tag_name('option'):
if option.text == value:
break
else:
ARROW_DOWN = u'\ue015'
elem.send_keys(ARROW_DOWN)
무엇이든 간에 전송하십시오 키를 사용하여 선택한 후 의해 찾기를 사용하여 xpath
단축시킵니다 예: HTML:
<select id="fruits01" class="select" name="fruits">
<option value="0">Choose your fruits:</option>
<option value="1">Banana</option>
<option value="2">Mango</option>
</select>
파이썬.
fruit_field = browser.find_element_by_xpath("//input[@name='fruits']")
fruit_field.send_keys("Mango")
'클래스' 셀레네기.웹드라이버자주포트드위지셀레스 사용하기 위한 최선의 방법을 함께 할 시간이 걸릴 것으로 인해 작동하지 않습니다 하지만 드롭다운이 선택물을 예상대로 설계 문제 또는 기타 문제를 HTML.
이 상황이 사용하여 다음과 같이 () '로' execute_script 뷰대체 솔루션이므로 포지셔닝하십시오 수도 있습니다. -
option_visible_text = "Banana"
select = driver.find_element_by_id("fruits01")
#now use this to select option from dropdown by visible text
driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", select, option_visible_text);
public class 리스트상자멀티플레 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("file:///C:/Users/Amitabh/Desktop/hotel2.html");//open the website
driver.manage().window().maximize();
WebElement hotel = driver.findElement(By.id("maarya"));//get the element
Select sel=new Select(hotel);//for handling list box
//isMultiple
if(sel.isMultiple()){
System.out.println("it is multi select list");
}
else{
System.out.println("it is single select list");
}
//select option
sel.selectByIndex(1);// you can select by index values
sel.selectByValue("p");//you can select by value
sel.selectByVisibleText("Fish");// you can also select by visible text of the options
//deselect option but this is possible only in case of multiple lists
Thread.sleep(1000);
sel.deselectByIndex(1);
sel.deselectAll();
//getOptions
List<WebElement> options = sel.getOptions();
int count=options.size();
System.out.println("Total options: "+count);
for(WebElement opt:options){ // getting text of every elements
String text=opt.getText();
System.out.println(text);
}
//select all options
for(int i=0;i<count;i++){
sel.selectByIndex(i);
Thread.sleep(1000);
}
driver.quit();
}
}