0

我正在执行的硒网络测试有问题。单击并转到下一页后,我不断收到过时的元素引用错误(这是正确的)


WebDriver driver = DriverFactory.getWebDriver()

WebElement PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

List<WebElement> Rows = PositionTable.findElements(By.tagName('tr'))

println('No. of rows: ' + Rows.size())


table: for (int i = 0; i < Rows.size(); i++) {
    
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    
   for (int j = 0; j < Cols.size(); j++) {
       if (Cols.get(j).getText().equalsIgnoreCase(ExpectedPosition)) {
           
           
          Cols.get(j).findElement(By.tagName('a')).click()
          WebUI.delay(5)

          table: break
          
          } 
          }
            
          }
4

1 回答 1

0

如果单击按钮更改页面 html 中的任何内容,则引用将丢失。你又找到了所有元素。所以改变你的代码如下

WebElement PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

List<WebElement> Rows = PositionTable.findElements(By.tagName('tr'))

println('No. of rows: ' + Rows.size())

Int countrow = 0 


table:     while(countrow<Rows.size) {
++countrow
PositionTable = driver.findElement(By.xpath('//td[2]/table/tbody/tr/td/table'))

Rows = PositionTable.findElements(By.tagName('tr'))
    
    List<WebElement> Cols = Rows.get(i).findElements(By.tagName('td'))
    Int countcol = 0 
    
   whil(countcol < Cols.size()) {
       ++countcol
       Cols = Rows.get(i).findElements(By.tagName('td'))
       if (Cols.get(j).getText().equalsIgnoreCase(ExpectedPosition)) {
           
           
          Cols.get(j).findElement(By.tagName('a')).click()
          WebUI.delay(5)

          table: break
          
          } 
          }
            
          }
于 2021-01-28T14:53:49.350 回答