2017-09-06 112 views
1

我是Selenium和GWT的初學者,我試圖測試添加了SmartGWT庫的Paint等GWT Web應用程序。我遇到了一個問題,試圖通過一種可與所有瀏覽器兼容的方法來定位網頁元素。我嘗試了很多定位元素的方法,但沒有一個能夠工作。首先,我嘗試使用無法識別的ID來查找元素。然後,我嘗試了絕對XPath的方法,它不適用於不同瀏覽器上的幾個元素。然後,我嘗試使用相對XPath查找這些元素,但不幸的是,該方法也出現了同樣的問題。然後,我找到了另一種使用sclocators來查找這些元素的方法,如下所示:Using Selenium in SmartGWT無法使用Selenium定位SmartGWT應用程序中的元素

我能夠使用此鏈接中提到的步驟在Selenium IDE中生成scLocators。但是當我在停止錄製之後在Selenium IDE中播放整個測試用例時,IDE本身無法找到這些元素,這些元素是我在執行各種操作(如點擊,編寫文本等時)時生成的。

此外,我還使用scLocators在Java中使用Selenium WebDriver來查找這些元素。但是,它並沒有發揮作用,並且沒有顯示出這樣的ELEMENT例外。

這是我的代碼。

public class RelativeXpath { 

public static void main(String[] args) throws InterruptedException { 
    System.setProperty("webdriver.chrome.driver", "D:\\SELENIUM\\Drivers\\chromedriver.exe"); 
    SmartClientWebDriver driver = new SmartClientChromeDriver(); 
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS); 
    Thread.sleep(3000); 
    driver.get("xxxxxx"); //here I have given the URL of my web application 
    WebDriverWait wait = new WebDriverWait(driver, 10); 
    wait.until(ExpectedConditions.titleContains("XXXX")); // here I've given the title of my web application 
    wait.until(ExpectedConditions.elementToBeClickable(By.id("frontCanvas"))); 
    Thread.sleep(4000); 
    WebElement Draw = driver.findElement(ByScLocator.scLocator("//HLayout[ID=\"long_ribbon_HLayout\"]/member[Class=IconMenuButton||index=1||length=20||classIndex=0||classLength=4||roleIndex=0||roleLength=15||scRole=button]/icon")); 
    Draw.click();             
    Thread.sleep(2000); 
    } 

} 

下面給出的是我想單擊的圖像+按鈕元素的HTML代碼。

<div id="isc_C" eventproxy="isc_IconButton_Client_0" role="button" aria-label="XXXX" style="position: absolute; left: 80px; top: 0px; width: 42px; height: 42px; z-index: 200090; box-sizing: border-box; overflow: hidden; cursor: pointer;" onscroll="return isc_IconButton_Client_0.$lh()"> 
    <div id="isc_D" eventproxy="isc_IconButton_Client_0" style="position: relative; display: inline-block; box-sizing: border-box; width: 100%; vertical-align: top; visibility: inherit; z-index: 200090; cursor: pointer;"> 
    <table role="presentation" width="42" height="42" cellspacing="0" cellpadding="0"> 
     <tbody> 
     <tr> 
      <td class="iconButton" style="padding: 4px; background-color: transparent;" valign="top" nowrap="true" align="center"> 
       <img src="XXXX.png" style="vertical-align:middle;margin-bottom:5px;" eventpart="icon" suppress="TRUE" draggable="true" width="32" height="32" border="0" align="TEXTTOP"> 
       <br> 
      </td> 
      </tr> 
     </tbody> 
    </table> 
</div> 
</div> 

我應該怎麼做才能解決這個問題?

+0

什麼是HTML佈局?將html代碼發佈到您的問題 –

+0

對不起,由於我公司的規則,我無法在此發佈HTML代碼。但我可以給你一些關於這個佈局的相關信息,你可以問。 – DG4

+0

是具有svg標籤的HTML嗎? –

回答

1

首先你下面的代碼是錯誤的

WebElement Draw = driver.findElement(ByScLocator.scLocator("//HLayout[ID=\"long_ribbon_HLayout\"]/member[Class=IconMenuButton||index=1||length=20||classIndex=0||classLength=4||roleIndex=0||roleLength=15||scRole=button]/icon")); 

您需要更換ByScLocator.scLocatorxpathcssSelector像下面

WebElement Draw = driver.findElement(By.xpath("//HLayout[ID=\"long_ribbon_HLayout\"]/member[Class=IconMenuButton||index=1||length=20||classIndex=0||classLength=4||roleIndex=0||roleLength=15||scRole=button]/icon")); 

現在你已經寫了XPath和你有共同的HTML是完全不同。

根據你的HTML,如果你想元素上點擊,就好像你的元素低於低於

<td class="iconButton" style="padding: 4px; background-color: transparent;" valign="top" nowrap="true" align="center"> 

然後使用XPath作爲

//td[@class='iconButton'] 

OR

//td[@class='iconButton' and @valign='top'] 

現在: -

<img src="XXXX.png" style="vertical-align:middle;margin-bottom:5px;" eventpart="icon" suppress="TRUE" draggable="true" width="32" height="32" border="0" align="TEXTTOP"> 

然後你可以使用: -

(//td[@class='iconButton']/img[@eventpart='icon'])[3] 

希望它會幫助你:)

+0

我嘗試將ByScLocator.scLocator替換爲xpath,就像你做的一樣,但它再次顯示相同的錯誤。除此之外,如果我將按照您的建議使用XPath「// td [@ class ='iconButton']/img [@ eventpart ='icon']」,那麼據我所知,它會點擊第一個圖標圖標與這些屬性,但我的圖標是列表中的第3或第4。 – DG4

+0

根據你所說的位置更新了我的xpath。目前它已被設置爲第三名,您可以根據自己的情況對其進行更改 –

+0

您的方法在Chrome,Firefox和Edge上運行良好,但它在Internet Explorer上無法正常工作。該怎麼辦?我在這裏發佈了這個問題:https://stackoverflow.com/questions/46094310/unable-to-locate-the-elements-of-smartgwt-application-using-selenium-in-ie-brows – DG4

相關問題