java - 'UnexpectedTagNameException' 和 Element 应该是 "select",但通过 Selenium java 使用 "div"函数却出现 'Select' 错误

标签 java selenium xpath webdriver webdriverwait

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

This is image

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

我为它编写代码

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

这给了我异常(exception)

'UnexpectedTagNameException'

错误消息是

Element should have been "select" but was "div"

HTML tags

最佳答案

此错误消息...

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

...暗示您已使用Select与元素交互的类,其中元素是 <div> .

click()在文本为 借贷能力 的元素上,您可以使用以下 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();
    
  • 浏览器快照:

BorrowingCapacity

关于java - 'UnexpectedTagNameException' 和 Element 应该是 "select",但通过 Selenium java 使用 "div"函数却出现 'Select' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55496154/

相关文章:

java - 将 Jtable 中的多行数据插入数据库

java - 在不同远程对象之间拆分远程 EJB 功能

java - 如何比较 selenium webdriver 中的 HTML 弹出窗口中是否存在部分文本?

java - 使用 Winium 实现 "Chrome Legacy Window"(Chromium) 的自动化

c# 并行运行 selenium 浏览器

c# - 如何在 C# 中将 xml 文件中的值赋给 datagridviewtextbox?

java - IntelliJ - 如何在整个项目中仅运行一个(主)类

java - JPQL 查询非实体集合

java - 无法在 div 类中找到元素

java - 元素不可见——XPath 仅查找样式