【问题标题】:Webelement get null values using selenium webdriverWebelement使用selenium webdriver获取空值
【发布时间】:2016-06-28 08:11:08
【问题描述】:

我正在尝试在 gmail 中打开邮件以验证“www.gmail.com”上的链接,并且已从“www.abc.com”完成注册,那么我们如何使用 selenium WebDriver 和关键字驱动来识别邮件框架。

我做了两种方法,一种方法包含所有带操作的关键字,另一种方法包含位置。

但无论何时执行,我都无法点击邮件打开,因为在 WebElement 中它会为空。

那么,谁能检查我是否正确声明了语法?或者为什么我没有得到价值?

代码如下:

private boolean operateWebDriver(String operation, String Locator,String value, String objectName) throws Exception {
    boolean testCaseStep = false;

    try {
        System.out.println("Operation execution in progress");
        WebElement temp = getElement(Locator, objectName);

        //Trying to click on email.
        if(operation.equalsIgnoreCase("Click_anElement")){

            if(temp.equals(value)){
                temp.click();
            }
        }

        //Trying to verify email.
        if (operation.equalsIgnoreCase("Verify_Email")) {
            System.out.println("Verify--->" + temp);
            if(temp.equals(value)){
                System.out.println("Verified");
            }
            else {
                System.out.println("Not Verified");
            }
        }
        testCaseStep = true;
    } catch (Exception e) {
        System.out.println("Exception occurred operateWebDriver"+ e.getMessage());

        // Take screenshot if any test-case is not working. 
        System.out.println("Taking Screen Shot");
        TakesScreenshot ts=(TakesScreenshot)driver;
        File source=ts.getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(source, new File("./Screenshots/"+screenshotName+".png"));
        System.out.println("Screenshot taken");
    }
    return testCaseStep;
}

public WebElement getElement(String locator, String objectName) throws Exception {
    WebElement temp = null;
    System.out.println("Locator-->" + locator);
    if (locator.equalsIgnoreCase("id")) {
        temp = driver.findElement(By.id(objectName));
    }
    else if (locator.equalsIgnoreCase("xpath")) {
        temp = driver.findElement(By.xpath(objectName));
        System.out.println("xpath temp ----->" + temp);
    }
    else if (locator.equalsIgnoreCase("xpath1")){
        String row_xpath_end= "]";
        for(int rowNum=1; rowNum<=10; rowNum++){
            temp = driver.findElement(By.xpath(objectName+rowNum+row_xpath_end));
        }
    }
    return temp;
}

错误:

Exception occurred operateWebDriverThe given selector /html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[7]/div/div[1]/div[2]/div/table/tbody/tr[ is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Unable to locate an element with the xpath expression /html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[7]/div/div[1]/div[2]/div/table/tbody/tr[ because of the following error:
[Exception... "The expression is not a legal expression."  code: "12" nsresult: "0x805b0033 (SyntaxError)"  location: "file:///C:/DOCUME~1/Chayan/LOCALS~1/Temp/anonymous1831932369561342639webdriver-profile/extensions/fxdriver@googlecode.com/components/driver_component.js Line: 5934"]
Command duration or timeout: 16 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'aa-ff7b4a68', ip: '192.168.1.3', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.7.0_71'
Session ID: 051985f9-6320-4d0f-ad48-0ab3ff975f02
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=18.0}]
Taking Screen Shot

【问题讨论】:

  • 是否输入getElement()中的if选项之一?
  • @Guy 实际上它无法在“temp = driver.findElement(By.xpath(objectName+rowNum+row_xpath_end));”行中找到元素。你能给它一些解决方案吗?我也发布了我得到的错误,请检查。
  • 您提供的xpathtr[ 结尾,因此您收到错误"The expression is not a legal expression."。你确定它不会进入第二个if吗? else if (locator.equalsIgnoreCase("xpath"))
  • @Guy 实际上它应该转到行 else if (locator.equalsIgnoreCase("xpath1")) 因为打开邮件的逻辑写在这里然后,它转到方法名称“operateWebDriver” . 在这些中获取值(邮件主题行),然后单击它。这是我正在尝试做的。
  • 附带说明,依赖像/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div[2]/div[1]/div[1]/div/div/div[7]/div/div[1]/div[2]/div/table/tbody 这样的xpath 是一个糟糕的主意。首先,页面上的任何细微变化都会被破坏,其次很难理解您要查找的内容等等。我会花时间寻找一些更符合逻辑的 xpath(例如,使用 id、class 一些属性,而不是 xpath 在页面上的位置)

标签: java selenium selenium-webdriver frameworks


【解决方案1】:

我已使用此代码从 gmail 获取密码 但您需要更新 xpath(检查一次)

WebElement tableelement=DriverObj.findElement(By.xpath("//div[@class='Cp']/div/table//tbody"));
            java.util.List<WebElement> tableelement1=tableelement.findElements(By.xpath("//div[@class='Cp']/div/table//tbody/tr"));
            int tableelementsize=tableelement1.size();
            int i;
            if(tableelementsize>0){
                //DriverObj.navigate().refresh();
                for(i=0;i<tableelementsize;i++){
                    DriverObj.findElement(By.xpath("//div[@class='Cp']/div/table//tbody/tr/td[6]")).click();
                }

            }else {
                 if(tableelementsize==0){
                    DriverObj.navigate().refresh();
                    for(i=0;i<tableelementsize;i++){
                        DriverObj.findElement(By.xpath("//div[@class='Cp']/div/table//tbody/tr/td[6]")).click();
                    }
                }
            }

【讨论】:

    【解决方案2】:

    您可以使用公共类xY a4W定位所有邮件并按索引点击

    List<WebElement> mails = driver.findElements(By.cssSelector(".xY.a4W")); // finds the mail body
    mails.get(0).click(); // open first mail
    

    您还可以找到来自特定发件人的所有邮件

    String name = "name";
    List<WebElement> mails = driver.findElements(By.xpath("//*[@class='zF'][@name='" + name + "']/../..");
    mails.get(0).click();
    

    这将打开第一封名为“name”的邮件。

    【讨论】:

    • 我们可以找到所有具有共同类的邮件,并在该类中生成每个邮件的不同ID,但在执行程序时它从“www.abc.com”注册帐户并在“www.gmail.com”收到一封邮件,那时我们如何识别该邮件并点击它?
    • 我们如何在不使用 List 的情况下做同样的事情,因为如您所见,我已将 WebElement 与他们的对象一起使用并返回它。那么,在一种方法中,我们如何返回两个类的对象呢?这是不可能的.. :(
    • @SelenyanC2 因为 id 是随机的,所以无法仅定位一个特定元素。您可以做的最好的事情是只从一个发件人那里获取电子邮件,然后使用索引 0 从那里获取第一封邮件。
    • 能否请您使用我正在做的方式提供示例代码,因为我正在尝试解决此问题但没有得到解决方案。提前谢谢..
    • @SelenyanC2 在getElement() 中将temp = driver.findElement(By.id(objectName)); 替换为temp = driver.findElements(By.xpath("//*[@class='zF'][@name='" + name + "']/../..").get(0);
    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多