webdriver classname with space using java

后端 未结 3 1722
栀梦
栀梦 2020-12-13 07:19

This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?

I\'m doing driver.findElement(

相关标签:
3条回答
  • 2020-12-13 08:09

    Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,

    WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
    element.click();
    

    Edit#1 Based on html provided,

    Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,

    WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
    element.click();
    

    or this should work too,

    WebElement element = driver.findElement(By.cssSelector("a.current.time"));
    element.click();
    

    You can also use linkText since the element is link. From the html you provided, the link text is 'url'

    WebElement element = driver.findElement(By.linkText("url"));
    element.click();
    

    You can also use By.partialLinkText("partial link text here");

    You can also use xpath as:

    WebElement element = driver.findElement(By.xpath("//a[@class='current time']"));
    element.click();
    

    OR,

    WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
    element.click();
    
    0 讨论(0)
  • 2020-12-13 08:09

    For a less fragile test, another option is to use an XPATH which doesn't depend of the order of classes, like:

    WebElement element = driver.findElement(By.xpath("//a[contains(@class, 'current') and contains(@class, 'time')]"));
    
    0 讨论(0)
  • 2020-12-13 08:13

    Whenever you found some space in the class name you need to switch to cssSelector Locator. Convert a class name to cssSelector if it is having a space as below.

    In your case it would be:

    WebElement element = driver.findElement(By.cssSelector(".current.time"));
    element.click();
    

    PS: add . [dot] in start of class name and replace the space with . [dot] to convert class name to cssSelector.

    0 讨论(0)
提交回复
热议问题