如何使用SeleniumWebDriver根据网页中的用户ID选择操作?

如何使用SeleniumWebDriver根据网页中的用户ID选择操作?,selenium,selenium-webdriver,Selenium,Selenium Webdriver,我想将excel中的Id与系统中的表进行比较。如果两个Id相同,则它将在表中选择一个操作。 它将转到下一页,直到找到用户Id并选择操作流程。看起来我的代码是错误的,但我不知道我需要更改哪个部分 driver.findElementBy.linktext.click; 字符串作者=sh1.getRow5.getCell0.getStringCellValue; String id=driver.findElementBy.xpath//td/table/tbody/tr/td/table/tbod

我想将excel中的Id与系统中的表进行比较。如果两个Id相同,则它将在表中选择一个操作。 它将转到下一页,直到找到用户Id并选择操作流程。看起来我的代码是错误的,但我不知道我需要更改哪个部分

driver.findElementBy.linktext.click; 字符串作者=sh1.getRow5.getCell0.getStringCellValue; String id=driver.findElementBy.xpath//td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/thead/tr/td[2]; 做{ ifid.equalsauthor{ Select view=new Selectdriver.findElementBy.xpath//Select[@name='selectedAction']; view.selectByVisibleTextView; driver.findElementBy.idreturnImage2.click; driver.findElementBy.linkTextLogout.click; 司机:关闭; 打破 } 否则{ driver.findElementBy.idimgListNext2.click;} } 虽然id.equalsauthor; } } 这是表格的屏幕截图。因此,根据我作为作者放入的excel中的ID,它将比较表User ID中的ID


首先,请检查您可用于当前页面和下一页面的元素ID是否相同,如果不相同,则需要为您所在的每个页面选择新ID,并且您可以选择一个布尔值,如果您的条件匹配,该布尔值将变为true,直到不满足条件,您可以继续使用while循环进行检查,如:

boolean match = false;
while(!match){     
  if(id.equals(author)){
  //Insert the select code here and set the boolean as true like:
  match = true;
  }
  else{ 
  driver.findElement(By.id("imgListNext2")).click();
  }
}

请让我知道它是否有用。

您在if和while中测试id==author两次,并且您没有在每个新页面上从页面上删除id,因为它在循环之外。像这样的事情应该会解决的

driver.findElement(By.linkText("S")).click();
String author = sh1.getRow(5).getCell(0).getStringCellValue();
while (!author.equals(driver.findElement(By.xpath("//td/table/tbody/tr/td/table/tbody/tr/td/table/tbody/tr/td/table/thead/tr/td[2]")).getText()))
{
    driver.findElement(By.id("imgListNext2")).click();
    // you will probably need to add some sort of wait here... wait for stale or the like to make sure the page has changed before you pull the ID again in the while
}

Select view = new Select(driver.findElement(By.xpath("(//select[@name='selectedAction'])")));
view.selectByVisibleText("View");
driver.findElement(By.id("returnImage2")).click();
driver.findElement(By.linkText("Logout")).click();
driver.close();

你看到什么错误了吗?在哪一行?错误说明了什么?不是可运行的代码段看起来id没有被比较,系统继续单击下一页。我猜该元素的xpath不正确,能否发送整个div层次结构,以便我可以帮助您使用xpathya我认为xpath是错误的,我尝试为表输入xpath并打印文本。它在表中显示了全部数据,但为什么它仍然无法读取正确的id。我无法复制html。请您将整个div结构的屏幕截图上传到imgur,然后共享它的url,好吗?我想我可以分享屏幕截图,但不能分享url,因为系统只能通过公司网络访问