有没有Selenium方法可以验证下拉框中写了什么?

huangapple go评论80阅读模式
英文:

Is there a Selenium method to verify what it is written in a drop down box?

问题

以下是您要翻译的内容:

我已经尝试验证在使用拖放操作移动元素后,下拉框中的内容。我尝试使用selectByVisibleText()方法,但我意识到这个方法并不相关。是否有人知道在Selenium中是否有适合我描述的方法?

以下是我的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class DragAndDrop {
    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver","/Chromedriver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("https://formy-project.herokuapp.com/dragdrop");

        WebElement seleniumLogo = driver.findElement(By.id("image"));
        WebElement box = driver.findElement(By.id("box"));

        Actions actions = new Actions(driver);
        actions.dragAndDrop(seleniumLogo, box).build().perform();

        new Select(driver.findElement(By.id("box"))).selectByVisibleText("");

        // driver.quit();
    }
}
英文:

I have tried to verify what it is written in a drop down box after the element is moved with drag and drop. I have tried using the selectByVisibleText() method but I realized this method is not relevant. Does anybody know if there is a method in Selenium that would fit my description?

Here is my code:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class DragAndDrop {
    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver","/Chromedriver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("https://formy-project.herokuapp.com/dragdrop");

        WebElement seleniumLoggo =driver.findElement(By.id("image"));
        WebElement box=driver.findElement(By.id("box"));

        Actions actions=new Actions(driver);
        actions.dragAndDrop(seleniumLoggo,box).build().perform();

       new Select( driver.findElement(By.id("box")).selectByVisibleText());

      // driver.quit();
    }
}

答案1

得分: 0

这实际上很简单(虽然可能不太直观)- 只需在元素上调用 getText()

String theText = driver.findElement(By.id("box")).getText();
英文:

It's actually trivial (though probably not very intuitive) - just call getText() on the element:

String theText = driver.findElement(By.id("box")).getText();

huangapple
  • 本文由 发表于 2020年8月29日 13:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63643875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定