如何通过Selenium Web Driver选择<select>标签上的项目?</select>

时间:2014-04-07 12:25:50

标签: testing xpath scripting automation selenium-webdriver

在发布此内容之前,我已经彻底研究了所有可用的语法并且无济于事。

我最接近的是使用此代码,其中下拉列表实际显示但未选择我想要的选项:

new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")));

driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")).sendKeys("Local Move"); 

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"))).click();

今天我花了很多时间来解决这个问题,但是我真的失败了。

3 个答案:

答案 0 :(得分:0)

从下拉列表中选择一个值。您需要使用网络驱动程序中的Select类。

driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")).sendKeys("Local Move");

而不是以上行使用此

WebElement ele = driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select"));
Select dropdown = new Select(ele);
dropdown.selectByVisibleText("Local Move");
// Can select the dropdown using `index` and `value`
dropdown.selectByValue("Local Move");
dropdown.selectByIndex("1234");

答案 1 :(得分:0)

WebElement dropDownListBox = driver.findElement(By.xpath("html/body/form/table/tbody/tr[4]/td/div/fieldset/table/tbody/tr/td[1]/table/tbody/tr[3]/td[2]/select")); 

Select clickThis = new Select(dropDownListBox);
Thread.sleep(5000L);
clickThis.selectByValue("1078");

似乎我需要Thread.sleep让系统在selenium找到我想要的选项之前完全显示选项。

谢谢你们!

答案 2 :(得分:0)

要从下拉列表中选择一个值,您应该按ID找到下拉元素,然后按下拉列表中的值找到。

试试这个:

HTTP/1.1 404 Not Foundset-cookie: test-usercontext=asdf-client=200; path=
/set-cookie: test=AjQxMDMBABhNAEEATgBHAE8AIAAgACAAIAAgACAAIAACAAYyADAAMAADABBGAEIAVAAgACAAIAAgACAABAAYMgAwADEANgAwADQAMgA4ADEAMAAzADQABQAEAAAACAYAAlgACQACRQD
%2fA%2fowggP2BgkqhkiG9w0BBwKgggPnMIID4wIBATELMAkGBSsOAwIaBQAwCwYJKoZIhvcNAQcBoIICwjCCAr4wggJ%21AgcgE
wUWFQk1MAkGByqGSM44BAMwRTELMAkGA1UEBhMCREUxDzANBgNVBAoTBlNBUC1BRzEMMAoGA1UECxMDRkJUMRcwFQYDVQQDDA4qL
ndkZi5zYXAuY29ycDAeFw0xMzA1MTYxNTA5MzVaFw0zODAxMDEwMDAwMDFaMEUxCzAJBgNVBAYTAkRFMQ8wDQYDVQQKEwZTQVAtQUcxDDAKBgNVBAsTA0ZCVDEXMBUGA1UEAwwOKi53ZGYuc2FwLmNvcnAwggG4MIIBLAYHKoZIzjgEATCCAR8CgYEA
%2f%21RvacHcYrHXGs0tkbb0V5pRZr%2166wpEefngcrA%2feo5VUyPFnjV%21VLPgYTlK5oVBT%21b8hVdVDF72%2fF7tG%2fzyNXjr6xCkACw69
%21NEBKQ4A7RSfLieN8IgCWl3ENA6cbgQJlhYMAugQexgN5rNX67eLyIFTEHGbF%2fkvlWXyDZt2AsCFQCCZiO7z37X8dHkeMyzasFRThlyFwKBgQC6
%2f4ZVeZJsMH528dpoLej8jEeUYstPpe6CFgIF7LqFjFnEWM%2fgt364k9%21kYRMdnN1l3Dgv2fHBOMET2lwiCSP%21Lp3JkDH9K
%21H9zLJfg%2fd3o5gNCu4pct9Au4%2fLh%21ejlgOAytMcr7z9G1SSaRKFk9QnzfyA4mmLmtDT6qZG%2fgx5%21wOBhQACgYEA%21rGBozVMIrQFy8mXlkOSDWiAo16gBBlLqAIhsc3Th
%2f6LFM%21HK8mzuxApdB8vDg5zOPQW959g2YmZBvMm7fBjjRyeQnh0SamBQNwdWR3cyhdXANRJTjdjV6yGJaBVUzMam5Yh2%2fNjwHWxS
%21Ev%21KNSUi7%2fHLrYjQ%2f5mYiipmBwOskwCQYHKoZIzjgEAwMvADAsAhQkPKrX2UIzsWHZoErU9zMKRjRMSwIUchu3vWrhXc2bckO
%21ASRCiCh3oF4xgf0wgfoCAQEwUDBFMQswCQYDVQQGEwJERTEPMA0GA1UEChMGU0FQLUFHMQwwCgYDVQQLEwNGQlQxFzAVBgNVB
AMMDioud2RmLnNhcC5jb3JwAgcgEwUWFQk1MAkGBSsOAwIaBQCgXTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMBwGCSqGSIb3D
QEJBTEPFw0xNjA0MjgxMDM0NTdaMCMGCSqGSIb3DQEJBDEWBBTLTQarxV7dyga8cQOmMUBQNBc4mTAJBgcqhkjOOAQDBC4wLAIUUOp
%21DiZu22HaQ324gaZTLbbjWqICFCtwUwws0te2HqBON0OF%2faQ%21hLD3; path=/; domain=test.de; secure; HttpOnlyset-cookie
: asdf_SESSIONID=VMoFGADsOTEThFHqCdDaC9mG0O4NLBHmhakAUFaDZVE%3d; path=/; secure; HttpOnlycontent-type
: application/jsoncontent-length: 504x-csrf-token: dZgtpkwUMaN-gQ1X4QEXUw==dataserviceversion: 1.0x-asdf-e2eperf
: { asName: "ldai1_00", asType: "asdf", elapsed: 319, maxMem: 4595, dbTime: 166 }{"error":{"code"
:"005056A509B11EE1B9A8FEC11C21D78E","message":{"lang":"en","value":"Resource not found for the segment
 'http:'."},"innererror":{"transactionid":"5721AEEC42F83183E10000000A4DA014","timestamp":"20160428103457
.6579540","Error_Resolution":{"error","Note":"Note 1797736
 for error analysis"}}}}<br>