Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 偶尔链接';s处理_Java_Selenium - Fatal编程技术网

Java 偶尔链接';s处理

Java 偶尔链接';s处理,java,selenium,Java,Selenium,主页包含300多个链接,点击主页上的每个链接会打开一个新的窗口(当然)和表格。我总是需要相同位置的表值,但是。。。有时(但仅有时)该(需要的)表值实际上也是一个链接,单击该链接可打开新窗口。 如果单击该表值打开新窗口(使用新表),我需要该新窗口中的特定表值,如果不需要(如果原始表值不是链接),我只需要原始表值。 我尝试使用下面的代码,但出现错误 线程“main”org.openqa.selenium.NoSuchElementException中的异常:无法找到元素:{“方法”:“xpath”,

主页包含300多个链接,点击主页上的每个链接会打开一个新的窗口(当然)和表格。我总是需要相同位置的表值,但是。。。有时(但仅有时)该(需要的)表值实际上也是一个链接,单击该链接可打开新窗口。 如果单击该表值打开新窗口(使用新表),我需要该新窗口中的特定表值,如果不需要(如果原始表值不是链接),我只需要原始表值。
我尝试使用下面的代码,但出现错误

线程“main”org.openqa.selenium.NoSuchElementException中的异常:无法找到元素:{“方法”:“xpath”,“选择器”:“//*[@id='aodds-info']]/div[2]/table/tbody/tr[3]/td[2]} 命令持续时间或超时:33毫秒

package newpackage;

   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileNotFoundException;
   import java.io.FileOutputStream;
   import java.io.IOException;
   import java.util.Iterator;
   import java.util.List;
   import java.util.NoSuchElementException;
   import java.util.Set;

   import org.openqa.selenium.By;
   import org.openqa.selenium.WebDriver;
   import org.openqa.selenium.WebElement;
   import org.openqa.selenium.firefox.FirefoxDriver;

   public class newdist {

    public static void main(String[] args) throws IOException,   InterruptedException {

    // Open main page

        WebDriver driver = new FirefoxDriver();
        driver.get("Main page link");
        Thread.sleep(5000);

        // Maximize main page window        
            driver.manage().window().maximize();
        Thread.sleep(1000);

           // List off all links on Main page
           List<WebElement> lista1 = driver.findElements(By.cssSelector(".first-cell.tl>a")); 

       // loop trough all links on Main page

        for(int j=0;j<lista1.size();j++){

        WebElement link = lista1.get(j);
        List<WebElement> links = driver.findElements(By.cssSelector(".first-cell.tl>a"));
        String homePage = driver.getWindowHandle();
        link.click();
        Thread.sleep(3000);

        // Window handles block

                Set<String>windows=driver.getWindowHandles();
        Iterator iterator = windows.iterator();
        String currentWindowId;
        while (iterator.hasNext()){
            currentWindowId = iterator.next().toString();
            if(! currentWindowId.equals(homePage)){
                driver.switchTo().window(currentWindowId);
                Thread.sleep(3000);

        // "clicking" on specific table value (clicking maybe opens new window)     

             driver.findElement(By.xpath(".//*[@id='sortable-1']/tbody/tr[6]/td[1]/span")).click();
             Thread.sleep(3000);

            // if clicking opens new window print specific value from table in that new window 

                try {   
            String s0 = driver.findElement(By.xpath(".//*[@id='aodds-info']/div[2]/table/tbody/tr[3]/td[2]")).getText();
            System.out.println(s0);
                     }

        // if clicking doesn't open new window print current table value from current window    

            catch (NoSuchElementException e){
            String s0 = driver.findElement(By.xpath(".//*[@id='sortable-1']/tbody/tr[6]/td[1]/span")).getText();
            System.out.println(s0);


        }   

          // return to Main page  
        finally{

            driver.close();
            driver.switchTo().window(homePage);
            Thread.sleep(2000);
            }
            }   
    }

}
}
}
package-newpackage;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Iterator;
导入java.util.List;
导入java.util.NoSuchElementException;
导入java.util.Set;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类新区{
公共静态void main(字符串[]args)引发IOException、InterruptedException{
//打开主页
WebDriver=newfirefoxdriver();
获取驱动程序(“主页链接”);
睡眠(5000);
//最大化主页窗口
driver.manage().window().maximize();
睡眠(1000);
//列出主页上的所有链接
List lista1=driver.findElements(By.cssSelector(“.first cell.tl>a”);
//通过主页上的所有链接进行循环

对于(int j=0;j而言,
NoSuchElementException
是从
java.util
导入的。要捕获web元素异常,需要从
org.openqa.selenium
导入


作为旁注,使用比使用
线程要好得多。sleep

忘记了,实际上它可以正常工作,但是当表值(从主页后的第一个窗口)不是链接时,Java返回错误…您可以添加带有错误的表的html吗?收集表值实际上并不是一个问题“需要使用新表值链接新窗口还是不需要链接收集现有值”…当您没有链接时,html可能不同我不明白,错误在
driver.findElement(By.xpath(./*[@id='sortable-1']/tbody/tr[6]/td[1]/span”)。单击();
driver.findElement(By.xpath(./*[@id='aodds-info']]/div[2]/table/tbody/tr[3]/td[2]”)。getText();
?是的,就是这样!非常感谢!现在看起来像是愚蠢的错误,但事实是你救了我一天…;)再次感谢。