簡體   English   中英

Webelement 使用 selenium webdriver 獲取空值

[英]Webelement get null values using selenium webdriver

我正在嘗試在 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

您可以使用 common class xY a4W找到所有郵件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”的第一封郵件。

我已使用此代碼從 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();
                    }
                }
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM