web - java selenium 設(shè)置顯式等待 沒(méi)有效果
問(wèn)題描述
最近使用的selenium,做個(gè)簡(jiǎn)單的自動(dòng)化工具,但是測(cè)試的時(shí)候感覺(jué) WebDriverWait wait = new WebDriverWait(driver,20);沒(méi)啥效果啊,在我進(jìn)入郵箱之后wait.until(ExpectedConditions.presenceOfElementLocated(By.id('_mail_component_68_68'))).click(); //打開(kāi)收件箱
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath('//*[@sign=’letter’][1]'))).click(); 這兩段代碼明明頁(yè)面已經(jīng)有這個(gè)元素了,為啥還要等一段時(shí)間才進(jìn)行操作,而且等待是時(shí)間也不是固定的,是我電腦問(wèn)題,很頭疼啊,演示的時(shí)候非常尷尬的等好長(zhǎng)時(shí)間。 selenium 用的3.4 火狐用的5.3
static {System.setProperty('webdriver.firefox.marionette','C:Program Files (x86)Mozilla Firefoxgeckodriver.exe');} //火狐瀏覽器驅(qū)動(dòng)
static String url='http://freemail.163.com/';//瀏覽器打開(kāi)的地址
public String openEmail(Stream<String> userNameSream,Stream<String> passwordStream) throws Exception {
long startTime=System.currentTimeMillis(); //獲取開(kāi)始時(shí)間 WebDriver driver = new FirefoxDriver(); //啟動(dòng)瀏覽器driver.manage().window().maximize(); //最大化瀏覽器WebDriverWait wait = new WebDriverWait(driver,20);driver.get(url);//打開(kāi)連接driver.switchTo().frame('x-URS-iframe');//定位到iframe框架中userNameSream.forEach(e -> driver.findElement(By.name('email')).sendKeys(e));//輸入賬號(hào)passwordStream.forEach(p -> driver.findElement(By.name('password')).sendKeys(p));//輸入賬號(hào)driver.findElement(By.id('dologin')).click();//點(diǎn)擊登錄Thread.sleep(3000);if(driver.getTitle().contains('網(wǎng)易郵箱')){ wait.until(ExpectedConditions.presenceOfElementLocated(By.id('_mail_component_68_68'))).click(); //打開(kāi)收件箱 wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath('//*[@sign=’letter’][1]'))).click(); return '成功,從程序開(kāi)始到打開(kāi)第一封郵件用時(shí)'+(System.currentTimeMillis()-startTime)/1000+'秒';}else{ try { WebElement loginMsg = driver.findElement(By.className('ferrorhead')); return loginMsg.getText(); }catch(Exception e){return '出現(xiàn)其他問(wèn)題'; }}
問(wèn)題解答
回答1:WebDriverWait是全部加載完才會(huì)執(zhí)行click
回答2:不知道為什么 但是解決了 我用的火狐 換谷歌之后就變得很快了
相關(guān)文章:
1. mysql - AttributeError: ’module’ object has no attribute ’MatchType’2. php自學(xué)從哪里開(kāi)始?3. javascript - 百度echarts series數(shù)據(jù)更新問(wèn)題4. MySQL客戶端吃掉了SQL注解?5. 求大神幫我看看是哪里寫(xiě)錯(cuò)了 感謝細(xì)心解答6. javascript - JS設(shè)置Video視頻對(duì)象的currentTime時(shí)出現(xiàn)了問(wèn)題,IE,Edge,火狐,都可以設(shè)置,反而chrom卻...7. javascript - 圖片能在網(wǎng)站顯示,但控制臺(tái)仍舊報(bào)錯(cuò)403 (Forbidden)8. python小白的基礎(chǔ)問(wèn)題 關(guān)于while循環(huán)的嵌套9. phpstady在win10上運(yùn)行10. python - Django分頁(yè)和查詢參數(shù)的問(wèn)題
