Selenium Webdriver,Javascript链接,无法定位元素

时间:2014-06-04 03:20:30

标签: java javascript selenium xpath webdriver

您好我正面临这个问题,下面是我使用Selenium IDE生成的代码,基本上我正在尝试访问下面特定网站的职业门户网站以及Jobposting QA专家,我正在尝试自动完成使用Selenium的应用程序。

1)尽管在适当的类下添加了代码并导入了所有必需的包,但我无法复制webdriver中的代码。 2)在将其作为TestNG测试运行时,我显示无法找到无法找到元素。 3)驱动程序未检测到QA专家的链接,如果我将其作为标识By.link文本或By.xpath。 4)请指导我在哪里犯错误。 5)我是Selenium的初学者

  public class Application {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.saymedia.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testApplication() throws Exception {
    driver.get(baseUrl + "/jobs");
    driver.findElement(By.linkText("QA Specialist")).click();
    driver.findElement(By.linkText("Apply Now")).click();
    driver.findElement(By.linkText("Send Application")).click();
  }

1 个答案:

答案 0 :(得分:0)

您的元素位于iframe内。 Selenium仅与当前帧中的元素交互。在您切换到frame之前,无法与子frame内的任何元素进行互动。您可以使用switchTo().frame()

进行切换
driver.get(baseUrl + "/jobs");
driver.switchTo().frame("jobviteframe");
driver.findElement(By.linkText("QA Specialist")).click();
driver.findElement(By.linkText("Apply Now")).click();
driver.findElement(By.linkText("Send Application")).click();

frame()的论据是

  • 从0开始的数字
  • frame
  • 的ID
  • webelement
  • frame重新引用

iframe完成后,使用以下内容退回到文档顶部:

driver.switchTo().defaultContent();