无法使用具有双下划线的className来标识WebElement。(Selenium WebDriver - JAVA)

时间:2017-08-15 13:33:25

标签: webdriver selector classname double-underscore

我一直在尝试使用className选择器识别一些WebElement,其中包含Double Underscore和Hyphens(例如: grid__row )但是当我运行脚本时我得到了

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element.

请参阅以下HTML内容的摘要。我突出了一些我无法识别的标签。

HTML Content

我不确定Double Underscore / Hyphen是否导致问题或其他问题。

找到我做的以下代码部分

package SiteCore_TestScript;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import SiteCore_Framework.Browser;

public class afhsSampleTest {

    WebDriver Driver;

  @BeforeTest
  public void Launchbrowser() {

      Driver = Browser.LaunchGoogleChrome("https://www.ashleyfurniturehomestore.com/c/furniture/living-room/sectional-sofas/?icid=meganav-furniture-menu-sectional-sofas");

  }

  @Test
  public void SelectProductInSubCatPg() throws InterruptedException{

      String ItemName = "Walworth Reclining Loveseat";

      WebElement MainProductList = Driver.findElement(By.className("main"));

      List<WebElement> ProductList = MainProductList.findElements(By.tagName("li"));

      for(int I = 0; I < ProductList.size(); I++){

          String ProductName = ProductList.get(I).getText();

          if(ProductName == ItemName){

              ProductList.get(I).click();

          }

          else if(ProductName != ItemName && I == (ProductList.size() - 1)){

              WebElement ProductContainer = MainProductList.findElement(By.className("container"));

              Thread.sleep(5000);

              WebElement GridCol = ProductContainer.findElement(By.className("grid__row")); //Throws exception in this line onwards.

              List<WebElement> AllProducts = GridCol.findElements(By.tagName("a"));

              for(int J =0; J < AllProducts.size(); J++){

                  String Sample = AllProducts.get(J).getText();

                  System.out.println(Sample);

//                if(AllProducts.get(J).getText() == "Click to View All Products"){
//                    
//                    Actions Act = new Actions(Driver);
//                    
//                    Act.moveToElement(AllProducts.get(J)).build().perform();
//                    
//                    AllProducts.get(J).click();
//                    
//                    J = AllProducts.size();
//                }
              }


          }

      }
  }

}

0 个答案:

没有答案