';未预料的例外情况';元素应该是;选择";但是是",;分区;使用'时出错;选择';函数通过SeleniumJava实现

';未预料的例外情况';元素应该是;选择";但是是",;分区;使用'时出错;选择';函数通过SeleniumJava实现,java,selenium,xpath,webdriver,webdriverwait,Java,Selenium,Xpath,Webdriver,Webdriverwait,在此表单中,下拉选择无效 在上图中,我想选择“借用容量” 我为它写代码 public static void main(String[] args) throws InterruptedException { WebDriver driver =new ChromeDriver(); //driver.manage().window().maximize(); driver.get("http://www.ia.ca/"); Thread.sl

在此表单中,下拉选择无效

在上图中,我想选择“借用容量”

我为它写代码

    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);
这给了我一个例外

“意外的AgName异常”

错误消息是

元素应为“select”,但为“div”

此错误消息

'UnexpectedTagNameException' : Element should have been "select" but was "div"
…表示您已使用
Select
类与元素交互,其中元素是

在文本为借用容量的元素上单击()

  • 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();
    
  • 浏览器快照:


当元素具有已定义的id时,为什么要使用长xpath?
Select cal=new Select(selectMyElement)
,但在此之前,
selectMyElement
是一个
div
->
driver.findElement(By.xpath(“…/div[2]/div[1]”)