首页 > 解决方案 > 无法为附加组件文本找到带有 xpath 的元素(正确的 xpath)

问题描述

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);       
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);         
driver.get("http://www.spicejet.com");  
Actions action = new Actions(driver); 
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
      .build().perform();       
Thread.sleep(3000);     
driver.findElement(By.linkText("Hot Meals ")).click();      
driver.close();

无法使用 xpath 定位元素 //a[contains(text(),'Add-Ons')

它与框架有关吗?

标签: seleniumxpath

解决方案


当我手动验证时,您的 xpath 可以找到正确的元素。我怀疑页面没有完全加载,请在调试sleep后添加一些。dirver.get()

driver.get("http://www.spicejet.com"); 

Thread.sleep(10*1000) // sleep 10 seconds to wait page open completely.

Actions action = new Actions(driver); 
action.moveToElement(driver.findElement(By.xpath("//a[contains(text(),'Add-Ons')]")))
      .build().perform(); 

推荐阅读