我正在尋找一種方法來檢查頁面是否使用Selenium加載。所以基本上是使用URL指向正確的地址(在Chrome瀏覽器中顯示是這樣的): 我如何檢查頁面是否加載了Selenium
我使用下面的代碼:
var driver = new FirefoxDriver();
driver.Url = "SomeUrl";
目前我比較標題爲「SomeUrl不可用「,如果匹配,則表明該頁面未能加載。
但是有沒有更好的方法?
我正在尋找一種方法來檢查頁面是否使用Selenium加載。所以基本上是使用URL指向正確的地址(在Chrome瀏覽器中顯示是這樣的): 我如何檢查頁面是否加載了Selenium
我使用下面的代碼:
var driver = new FirefoxDriver();
driver.Url = "SomeUrl";
目前我比較標題爲「SomeUrl不可用「,如果匹配,則表明該頁面未能加載。
但是有沒有更好的方法?
我個人試圖在頁面上找到body
或frameset
元素。
根據我在使用硒和鉻合金的經驗,頁面內容有時不顯示,我需要運行webdriver.refresh()
1-2次。它不會一直髮生,但我在一臺機器上運行大量的chrome實例,這種方法對於避免這種情況是最有效的。
因此,我試圖找到body
或frameset
元素,並在我放棄之前重新加載幾次。
該解決方案的優點還在於,它還可以在Chrome將來改變其消息的情況下工作。它也可以與其他瀏覽器一起使用。
錯,'ggggggsssggg.com'會有'
' – Toolkit我會建議,如果你知道什麼控制ID /或鏈接或/類將出現在您的網頁上。然後等待該元素在屏幕上顯示一段指定的時間。
我該元素出現那麼你的下行代碼將被執行
這裏是米方法的簡單版本。根據您的需要,您可以使其更具動態性。
/// <summary>
/// WaitForElementOnPage() method waits a passed number of seconds for the existence
/// of a passed IWebElement object. If the object is found the method return an IWebElement of the object
/// If the object is not found in the passed number of seconds, the method returns a null IWebElement
/// </summary>
public IWebElement WaitForElementOnPage(string controlID, int waitTime)
{
IWebElement element = null;
IWebElement elementFound = null;
try
{
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(waitTime));
Thread.Sleep(3000);
elementFound = wait.Until<IWebElement>((d) =>
{
element = d.FindElement(By.Id(controlID));
break;
return element;
});
}
catch (Exception ex)
{
Log.Error(ex);
elementFound = null;
}
return elementFound ;
}
如果我不這樣做? – Toolkit
這是最好的方法。它的速度快,可讀性強,除非一個網頁設置它的標題,否則不會失敗。 –