Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing 如何通过Selenium Web驱动程序选择标记上的项目?_Testing_Xpath_Scripting_Automation_Selenium Webdriver - Fatal编程技术网

Testing 如何通过Selenium Web驱动程序选择标记上的项目?

Testing 如何通过Selenium Web驱动程序选择标记上的项目?,testing,xpath,scripting,automation,selenium-webdriver,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]

在发布这篇文章之前,我已经彻底研究了所有可用的语法,但没有结果

我最近使用的代码是,下拉列表实际上出现了,但没有选择我想要的选项:

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();

今天我花了大量的时间来弄清楚这一点,但在这一重大时刻我真的失败了

从下拉列表中选择一个值。您需要使用“从web驱动程序中选择类”

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");
似乎我需要Thread.sleep让系统完全显示选项,然后selenium才能找到我想让它选择的选项


谢谢大家

要从下拉列表中选择一个值,您应该在下拉列表中按id然后按值查找下拉列表元素

试试这个:

new Select(driver.findElement(By.id("Dropdownid"))).selectByVisibleText("Text name");

尝试使用标签的id、class、name..等属性来避免如此长的XPath。这通常会导致您的测试用例失败,而且更短的XPath或选择器会提高可读性。也许这回答了我的下一个问题,为什么有时候我的测试用例失败,有时候它不会。非常感谢。尽管我观察到有时,在一些字段上没有ID或Name属性,因此我强烈依赖XPath。你好我试过了,结果和我以前试过的一样。执行时出现错误:-线程main org.openqa.selenium.NoSuchElementException中的异常:无法使用文本定位元素:如果Web驱动程序无法在DOM中找到元素,则将引发Local MoveNoSuchElementException。您可以使用+并等待暂停执行,直到DOMI中的元素最近解决此问题问题,我只是使用Thread.sleep来等待丢失的元素完全加载。但在我不久前的研究中,我发现使用“等待”/“流畅等待”比使用线程更可取。由于某些元素的加载时间可能比预设的线程睡眠时间长,因此使用睡眠
new Select(driver.findElement(By.id("Dropdownid"))).selectByVisibleText("Text name");