0

我正在寫硒驅動程序腳本。腳本的場景是:
1)登錄www.yahoomail.com
2)成功登錄。
3)點擊「撰寫」按鈕
4)在「收件人」字段中輸入電子郵件ID。
5)輸入主題「主題領域。
6)在輸入電子郵件正文 「文本」 字段。
7)點擊 「發送」 按鈕。
如何處理每次動態更改的Xpath?

使用Firepath,我已經採取了上述領域的的XPath 。在「撰寫」頁面 但每次當我打開撰寫頁面時,這種動態的XPath變化

下面是我使用的腳本:

WebDriver oYahoo = new FirefoxDriver(); 
    oYahoo.get("http://www.yahoomail.com/"); 
    oYahoo.manage().window().maximize(); 
    oYahoo.findElement(By.xpath(".//*[@id='login-username']")).sendKeys("[email protected]"); 
    oYahoo.findElement(By.xpath(".//*[@id='login-passwd']")).sendKeys("[email protected]"); 
    oYahoo.findElement(By.xpath(".//*[@id='login-signin']")).click(); 
    oYahoo.findElement(By.xpath(".//*[@id='Compose']/button")).click(); 
    oYahoo.findElement(By.xpath(".//*[@id='yui_3_16_0_1_1448364357109_2222']")).sendKeys("[email protected]"); 
    oYahoo.findElement(By.xpath(".//*[@id='subject-field']")).sendKeys("Hi This is my first automated mail"); 
    oYahoo.findElement(By.xpath(".//*[@id='yui_3_16_0_1_1448364357109_1966']")).sendKeys("Hi This is my first automated mail"); 
    oYahoo.findElement(By.xpath(".//*[@id='yui_3_16_0_1_1448364357109_2465']")).click(); 
    oYahoo.quit(); 

我們可以這樣做它會動態地使用Xpath?我們是否需要在某些課堂上繪製這些東西?我們是否必須爲「撰寫」彈出窗口編寫任何其他類? 如果「是」,那我們該怎麼做呢?

+0

這是自動化的基本問題。我建議你做一些事情,比如找到像「yui_ *」這樣的id的「第一個」元素。或者找到一個你可以找到其子的基本元素。 – DMart

回答

0

嘗試觀察每次重複的常見模式。無論是一個ID前綴或類前綴,任何東西。

然後在您的By.xpath(..)函數中使用以下選擇器。

//*[contains(@id,’your-common-pattern’)]

+0

它適合你嗎? – Abhinav

0

我在一個項目中,每次生成頁面時都會隨機輸入一個類名,以及我發現獲取這些標籤/輸入/的唯一方式。是我使用了帶css選擇器的nth-child()函數。思維方法是這樣的:我想要從第一個表的第二行開始的第三個輸入。

結帳這個小提琴來了解我的意思。 https://jsfiddle.net/8v150af1/