我在使用C#中的PhantomJS/Selenium WebDriver自動化網站時遇到了一些問題。C# - PhantomJS/Selenium - Click事件不註冊在模式內的按鈕上
部分網站有地址檢查;輸入一個它無法驗證的地址(或者根本不輸入地址)就會彈出一個帶有按鈕的模式div。根據具體情況,它會爲您提供最近的地址,或者可以選擇不使用地址而繼續,但該按鈕始終具有相同的ID。對於這個div的HTML(爲「無地址進入」版本)是在這裏:
<div id="confirmAddress" class="col-xs-5 active-popupbox">
<div class="row vert-offset-top-2">
<p style="margin-left:55px;" class="h3">No Customer Address Provided</p>
<br>
<div style="margin-left:42px;margin-bottom:0px;" class="modal-body">No Customer Address Was Entered. We recommend having the Customer provide an address.</div>
<br>
<div class="col-xs-6">
<button type="submit" class="btn btn-default" style="margin-left:45px;width:200px;" id="confirmBack" name="confirmBack" align="center">Enter Address</button> </div>
<div class="col-xs-6">
<button type="submit" class="btn btn-default" id="confirmPopUp" name="confirmPopUp" align="center">Proceed with No Address</button>
</div>
</div>
</div>
的按鈕,我需要點擊是最後一個按鈕,<button type="submit" class="btn btn-default" id="confirmPopUp" name="confirmPopUp" align="center">Proceed with No Address</button>
然而,儘管看似能夠找到元素在我的代碼和調用它的Click(),抓取截圖顯示它顯然沒有註冊點擊,因爲模態彈出窗口停留在頁面上,並且由於沒有找到我要求的元素找。
這裏是我的代碼的相關部分:
File.WriteAllText(@"C:\Users\user1\Documents\Testing\source0.html",
_driver.PageSource);
_driver.FindElementById("contactinfo_next").Click();
if (_driver.FindElementByClassName("active-popupbox").Displayed)
{
_driver.GetScreenshot().SaveAsFile(@"C:\users\user1\documents\testing\screenshot01.png", ImageFormat.Png);
_driver.FindElementById("confirmPopUp").Click();
}
_driver.GetScreenshot().SaveAsFile(@"C:\users\user1\documents\testing\screenshot02.png", ImageFormat.Png);
我用的類名來查找DIV,因爲他們顯然有着不同的id取決於是否這個地址是「不提供」或「不可以證實,但發現了一個近似匹配「,但是班級總是一樣的。
我點擊「contactinfo_next」按鈕之前,我有PhantomJS抓取信息源,這是什麼調出模態div,並確認沒有與class「active-popupbox」存在div單擊contactinfo_next按鈕之前單擊。 「screenshot01.png」確實得到保存,這意味着它正在尋找div ...但它仍然沒有註冊按鈕點擊。
我試過點擊div中的其他元素使它「成爲焦點」,在按鈕上調用Click()兩次,將div賦值給一個名爲div的變量,然後使用div.FindElements(By.Id("confirmPopUp"))
找到該按鈕並單擊每個元素在收集中,沒有任何工作。
有沒有其他人遇到過這個問題?
謝謝!
編輯:我用下面的NuGet包這樣的:
- PhantomJS
- 硒的webdriver PhantomJS跨平臺
- 硒的webdriver支持類
- 硒的webdriver
你如何使用PhantomJS類似的問題?你寫了自己的綁定,還是使用別人的? – SiKing
糟糕,可能應該提到這一點。使用NuGet的「Selenium WebDriver PhantomJS跨平臺」軟件包 – ZhangC1459
我不知道如何找出使用哪個版本的PhantomJS。但這可能很重要。 – SiKing