簡體   English   中英

如何使用XPath不是常數的Selenium WebDriver Java單擊按鈕

[英]How to click button using selenium webdriver java, whose xpath is not constant

我是Java中的Selenium Webdriver的新手。 我在Firefox Quantum(59.0,x64)上使用了webdriver。 我面臨的問題是我正在編寫用於測試的代碼的網站沒有恆定的xpath。 每次訪問時,按鈕ID都會更改。 所有4個啟動按鈕的類名(啟動按鈕)都相同。 因此,在編寫代碼時,它最初起作用並打開了第一個鏈接,但是對於第二個鏈接,它又打開了第一個鏈接,因為所有四個按鈕的類名都相同。 請通過按其他啟動按鈕來幫助我進行編碼,以打開其他鏈接。

我使用此(下面)代碼,但它部分起作用。 請幫幫我,因為我也想使用其他主題的按鈕來啟動。 (請參閱屏幕截圖) 這里的 checkout screenshot1這里的checkout screenshot2

我也不能在選項卡之間切換。 請幫助。 謝謝

driver.findElement(By.className("launchbutton")).click();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.get("http://google.com");
//Grab all elements which have the className "launchbutton"
List<WebElement> elements = driver.findElements(By.className("launchbutton"));

//Iterate your list of matching elements and look at the innerText for the button you want
//When found, click it and break the for loop
foreach(WebElement ele in elements)
{
    if(ele.getAttribute("innerText") == "The button text you want")
    {
         ele.Click();
         break;
    }
}

您可以點擊如下所示的啟動按鈕,然后根據所提及的注釋更改代碼

        //Specify the Expected Name in the below String
        String subjectName="";

        //Find the Table Element using any one of the Unique Locator
        WebElement table=driver.findElement(By.id(""));
        List<WebElement> rowElementList=table.findElements(By.xpath(".//tr"));

        //I have assumed Launch button column in 5th.So, Specified as td[4].
        //Please cross check the Launch button column and change the index accordingly
        for(WebElement element:rowElementList){
            if(element.findElement(By.xpath("./td")).getText().equalsIgnoreCase(subjectName)){
                element.findElement(By.xpath("./td[4]/a")).click();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM