需要帮助使用webdriver和java识别灯箱上的控件

时间:2015-10-05 22:56:19

标签: java selenium-webdriver webdriver lightbox

我正在尝试自动化网站流量的登录。当我点击登录时,弹出一个灯箱,我的脚本无法识别弹出窗口内的元素。该网站是www.zillow.com,当我点击右上角的登录链接时会发生这种情况。

我尝试使用JavascriptExecutor,等待和更多的方法,但无法识别。在这需要帮助。谢谢!!我得到的错误如下。

public class Trial {

    public static void main(String [] args){

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.zillow.com");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//*[@id='login_opener']/span")).click();
        driver.findElement(By.xpath("//*[@id='email']")).sendKeys("abcde");
        driver.findElement(By.xpath("//*[@id='password']']")).sendKeys("abcde");
    }
}

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='email']"}
Command duration or timeout: 20.51 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'pc-linux-ravip', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-65-generic', java.version: '1.7.0_79'
*** Element info: {Using=xpath, value=//*[@id='email']}
Session ID: 3b861869-4a8b-4cd1-b051-bab213d02077
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=41.0.1}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:358)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
    at com.trials.testng.learning.Trial.main(Trial.java:18)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='email']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'pc-linux-ravip', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-65-generic', java.version: '1.7.0_79'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///tmp/anonymous4582481678202576888webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10667)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///tmp/anonymous4582481678202576888webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)

2 个答案:

答案 0 :(得分:0)

我可以看到你的Xpath格式错误。我稍微修改了你的代码并且工作正常

function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) { 
$mr = $maxredirect === null ? 5 : intval($maxredirect); 
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); 
    curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); 
} else { 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
    if ($mr > 0) { 
        $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 

        $rch = curl_copy_handle($ch); 
        curl_setopt($rch, CURLOPT_HEADER, true); 
        curl_setopt($rch, CURLOPT_NOBODY, true); 
        curl_setopt($rch, CURLOPT_FORBID_REUSE, false); 
        curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); 
        do { 
            curl_setopt($rch, CURLOPT_URL, $newurl); 

            $header = curl_exec($rch); 
            if (curl_errno($rch)) { 
                $code = 0; 
            } else { 
                $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); 
                if ($code == 301 || $code == 302 || $code == 303) { 
                    preg_match('/Location:(.*?)\n/', $header, $matches); 
                    $newurl = trim(array_pop($matches)); 
                } else { 
                    $code = 0; 
                } 
            } 
        } while ($code && --$mr); 
        curl_close($rch); 
        if (!$mr) { 
            if ($maxredirect === null) { 
                trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); 
            } else { 
                $maxredirect = 0; 
            } 
            return false; 
        } 
        curl_setopt($ch, CURLOPT_URL, $newurl); 
    } 
} 
return curl_exec($ch); 
} 

function get_url($url) {

$ch = curl_init();
$timeout = 120;

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec_follow($ch);
print_r(curl_getinfo($ch));

return $data;

curl_close($ch);

if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
}

我更改了跟踪xpat​​h并在点击登录按钮

后引入了一个 WebDriver driver = new FirefoxDriver(); driver.get("http://www.zillow.com"); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.xpath("//span[contains(text(),'Sign in')]")).click(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); driver.findElement(By.xpath("//input[@id='email']")).sendKeys("abcde"); driver.findElement(By.xpath("//input[@id='password']")).sendKeys("abcde");
  1. implicit-wait识别登录按钮。
  2. //span[contains(text(),'Sign in')]识别电子邮件文本框
  3. //input[@id='email']识别密码文本框

答案 1 :(得分:0)

实际上,弹出框是在框架中,并且特定框架上没有id或名称。

WebDriver driver = new FirefoxDriver();
                driver.get("http://www.zillow.com");
                driver.manage().window().maximize();
                Thread.sleep(5000);
                driver.findElement(By.xpath("//span[contains(text(),'Sign in')]")).click();
                Thread.sleep(10000);
                List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
                System.out.println("The total number of iframes are " + iframeElements.size());
                for(int i=0;i<iframeElements.size();i++)
                {
                try
                {
                System.out.println("Entered Try");
                driver.switchTo().frame(i);
                System.out.println("Switched to frame");
                Thread.sleep(3000);
                driver.findElement(By.xpath("//input[@id='email']")).sendKeys("abcde");
                driver.findElement(By.xpath("//input[@id='password']")).sendKeys("abcde");
                }
                catch(Exception e)
                {
                System.out.println("Not in this frame:"+ i);
                driver.switchTo().defaultContent();
                }
                System.out.println("Out of catch");
                }

我从网页上获取所有帧并将其保存在List<WebElement>并使用for循环进行导航。

通过使用帧索引,我处理帧,即driver.switchTo().frame(i); i是索引