Selenium WebDriver can't find element by link text

前端 未结 5 1366
死守一世寂寞
死守一世寂寞 2021-01-04 09:44

I\'m trying to select an element that includes an anchor, but the text is buried in a paragraph inside of a div. Here\'s the HTML I\'m working with:



        
相关标签:
5条回答
  • 2021-01-04 10:03

    A CSS selector approach could definitely work here. Try:

    driver.findElement(By.CssSelector("a.item")).Click();
    

    This will not work if there are other anchors before this one of the class item. You can better specify the exact element if you do something like "#my_table > a.item" where my_table is the id of a table that the anchor is a child of.

    0 讨论(0)
  • 2021-01-04 10:04

    This doesn't seem to have <a> </a> tags so selenium might not be able to detect it as a link.

    You may try and use

    driver.findElement(By.xpath("//*[@class='ng-binding']")).click();
    

    if this is the only element in that page with this class .

    0 讨论(0)
  • 2021-01-04 10:12

    Use xpath and text()

    driver.findElement(By.Xpath("//strong[contains(text(),'" + service +"')]"));
    
    0 讨论(0)
  • 2021-01-04 10:13

    The problem might be in the rest of the html, the part that you didn't post.

    With this example (I just closed the open tags):

    <a class="item" ng-href="#/catalog/90d9650a36988e5d0136988f03ab000f/category/DATABASE_SERVERS/service/90cefc7a42b3d4df0142b52466810026" href="#/catalog/90d9650a36988e5d0136988f03ab000f/category/DATABASE_SERVERS/service/90cefc7a42b3d4df0142b52466810026">
    <div class="col-lg-2 col-sm-3 col-xs-4 item-list-image">
    <img ng-src="csa/images/library/Service_Design.png" src="csa/images/library/Service_Design.png">
    </div>
    <div class="col-lg-8 col-sm-9 col-xs-8">
    <div class="col-xs-12">
        <p>
        <strong class="ng-binding">Smoke Sequential</strong>
        </p>
        </div>
    </div>
    </a>
    

    I was able to find the element without trouble with:

    driver.findElement(By.linkText("Smoke Sequential")).click();
    

    If there is more text inside the element, you could try a find by partial link text:

    driver.findElement(By.partialLinkText("Sequential")).click();
    
    0 讨论(0)
  • 2021-01-04 10:26
    find_elements_by_xpath("//*[@class='class name']")
    

    is a great solution

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