【问题标题】:'UnexpectedTagNameException' and Element should have been "select" but was "div" error using 'Select' function through Selenium java'UnexpectedTagNameException' 和 Element 应该是“select”,但通过 Selenium java 使用“Select”函数时出现“div”错误
【发布时间】:2019-08-25 00:59:53
【问题描述】:

在此表单中,下拉菜单的选择不起作用。

在上图中我想选择“借贷能力”

我为它写代码

    public static void main(String[] args) throws InterruptedException 
    {
    WebDriver driver =new ChromeDriver();
    //driver.manage().window().maximize();
    driver.get("http://www.ia.ca/");
    Thread.sleep(3000);
    driver.findElement(By.xpath("//*[@id=\"nav-secondaire\"]/div[1]/ul/li[4]/a")).click();
    driver.findElement(By.xpath("//*[@id=\"nav-secondaire\"]/div[1]/ul/li[4]/ul/li[1]/section/ul/li[1]/a")).click();

   //DropDown code
    WebElement selectMyElement =driver.findElement(By.xpath("//*[@id=\"grille-zone-cta\"]/div/div/div/div/div/div[2]/div[1]"));
    Select cal = new Select(selectMyElement);
    cal.selectByIndex(1);

它给了我例外

'UnexpectedTagNameException'

错误信息是

元素应该是“select”,但是是“div”

【问题讨论】:

  • 当元素有定义的id时为什么要使用长xpath?
  • Select cal = new Select(selectMyElement);,但在此之前,selectMyElementdiv -> driver.findElement(By.xpath(".../div[2]/div[1]"));

标签: java selenium xpath webdriver webdriverwait


【解决方案1】:

此错误消息...

'UnexpectedTagNameException' : Element should have been "select" but was "div"

...暗示您已使用Select 类与元素交互,因为元素是<div>

对元素上的click(),文本为Borrowing Capacity,您可以使用以下Locator Strategy

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//h4[@class='bta-description' and text()='Our calculators']//following::div[@class='bta-select-table row']//b[@class='button']"))).click();
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='selectric-items']//li[contains(., 'Borrowing Capacity')]"))).click();
    
  • 浏览器快照:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多