2017-08-03 37 views
1

Selenium WebDriver能夠幫助誰工作?Selenium代碼不能用於在Safari中定位元素,在Firefox和Chrome中工作

我一直試圖在Mac機器上使用Selenium WebDriver自動化測試場景。當我將Safari定義爲我的瀏覽器時,即使在java代碼問題/錯誤頁面上存在元素,我也會收到錯誤"An element could not be located on the page using the given search parameters"

說明:當我們選擇Firefox和Chrome作爲瀏覽器時,可以找到相同的元素。有一些類似的答案,但他們都沒有談論Safari瀏覽器和Mac機器。

+1

嘿肖恩!就目前而言,除非您還添加了一些代碼(發佈代碼段!),否則您的問題很難調試。請點擊** [編輯](https://stackoverflow.com/posts/45479254/edit)** 你的問題,並確保你有一個有效的** [最小,完整和可驗證的例子](https:// stackoverflow .com/help/mcve)**, 否則,絕大多數答案都會是黑暗中的鏡頭。 – iamdanchiv

回答

0

試着再等一等。使用流利的等待如下: -

WebElement waitsss(WebDriver driver, By elementIdentifier){ 
    Wait<WebDriver> wait = 
       new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) 
               .pollingEvery(1, TimeUnit.SECONDS) 
               .ignoring(NoSuchElementException.class); 

    return wait.until(new Function<WebDriver, WebElement>() 
      { 
       public WebElement apply(WebDriver driver) { 
         return driver.findElement(elementIdentifier); 
       } 
       }); 
} 

等待應該爲你工作。如果問題仍然存在,則使用JavascriptExecutor。它將通過JS直接操作。它應該工作。我給一個例子,點擊任一元素使用JavascriptExecutor

WebElement element = driver.findElement(By.id("gbqfd")); 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", element); 

希望它會幫助你:)

+0

嗨Shubham,我嘗試了它,它工作。真棒! –

+0

我爲你工作。請點擊答案左側的正確符號接受答案,就在下面的投票中。請投票了..它將真正幫助我和其他人誰將在未來提及這個問題..這是如何stackoverflow工作:) ...參考: - https://meta.stackexchange.com/questions/5234/如何接受答案的工作 –

+0

我是非常新的stackoverflow :)當我試圖投票,我得到的消息:「感謝您的反饋!投票案例小於15聲譽的人被記錄,但不要更改公佈顯示的分數!「 - 因此,我很樂意與我分享任何建議。再次感謝你。 –

相關問題