2016-07-15 22 views
0

使用的webdriver的raw_input和(),选择下拉列表值这里的工作代码:如何在python

driver.find_element_by_xpath("//select[@name='log']/option[text()='eag']").click() 

它选择了“东亚运动会”的价值,但我想利用用户的raw_input选择()的值。例如。

choice = raw_input("choose: ") 

,然后使用该变量来选择已经设定值;)

有没有人有一个想法,怎么办呢?

在此先感谢!

+0

为什么你想做的事这个?我知道你想在测试脚本执行时手动提供测试数据。我是对的吗? – kotoj

+0

我需要解析页面的内容,但首先我必须选择和输入值;)我想从控制台而不是从代码 – pingwin850

回答

1

以下代码应该有所帮助。

choice = raw_input("choose: ") 
    driver.find_element_by_xpath("//select[@name='log']/option[text()='" + choice + "']").click() 
0

为了更好的办法,你应该使用Select()选择从下拉选项下,如下: -

from selenium.webdriver.support.ui import Select 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 

wait = WebDriverWait(driver, 10) 


element = wait.until(EC.visibility_of_element_located((By.NAME, "log"))) 

select = Select(element) 

choice = raw_input("choose: ") 

select.select_by_visible_text(choice) 

希望它能帮助... :)