click on a <div class >link using WebDriver

36,186

Use:

driver.findElement(By.className("AddContentBTN")).click();

In case you don't know, documentation for Selenium's "By" class can be found here.

Share:
36,186
Insane
Author by

Insane

Updated on August 26, 2022

Comments

  • Insane
    Insane almost 2 years

    I am not able to figure out how to click the link within div***

    Below is my page resource, xpath, css & my failure attempts. With each of the attempts, I received org.openqa.selenium.NoSuchElementException: no such element...

    Help?

    <form id="SearchAllCampaignsForm" action="/as/update.do" method="post" name="SearchAll">
     <style>
      <script>
       <div class="body-content">
        <input type="hidden" value="" name="campCookie">
         <div id="container" class="Coptions">
          <div class="containcamps">
           <div id="dwrapper" class="dTables_wrapper" role="grid">
            <div class="owrapper">
             <div class="refresh">
              <div class="addRow">
            ***<div class="AddContentBTN" title="Add New" rel="createCampaign">Add New</div>***
              </div>
    </form>
    

    My attempts:

    @Test
     public void addCamp(){
     //WebElement link = driver.findElement(By.linkText("Add New"))
    
     //driver.findElement(By.xpath("//div[@class='AddContentBTN']/rel[text()='createCampaign']")).click();
    
     //driver.findElement(By.xpath("//a[@title = 'Add New']")).click();
     //Actions builder = new Actions(driver);
     //builder.moveToElement(addCamp).click().perform();
     //driver.findElement(By.cssSelector("div.wrapper div.row-fluid form#SearchAllCampaignsForm div.body-content div.container div#dataTable_wrapper.dataTables_wrapper div.optionswrapper div.addRow div.AddContentBTN")).click();
    }
    

    xPath and CSS:

    /html/body/div[3]/div/form/div/div[2]/div/div/div[2]/div
    
    html body div.wrapper div.row-fluid form#SearchAllCampaignsForm div.body-content div.container div#dataTable_wrapper.dataTables_wrapper div.optionswrapper div.addRow div.AddContentBTN
    
  • Insane
    Insane over 10 years
    I tried with/without WebElement, but no luck...WebElement link = driver.findElement(By.className("AddContentBTN")); Actions builder = new Actions(driver); builder.moveToElement(link).click().perform();
  • Ben Smith
    Ben Smith over 10 years
    Sorry, I changed the code to "className" not "ClassName". Try it now.
  • Insane
    Insane over 10 years
    Not working... Interestingly, I used the same code to test another link on the same page, and it works. I think this code works because the link is a subclass of div class... WebElement lOut = driver.findElement(By.className("logout")); Actions builder = new Actions(driver); Action SeriesofActions = builder.moveToElement(lOut).click().build(); SeriesofActions.perform();
  • Insane
    Insane over 10 years
    The xpath for the above working click event /html/body/div[3]/div/div[2]/ul/li[4]/a
  • Ben Smith
    Ben Smith over 10 years
    I dont think you need to use "moveToElement", as you don't need to move the mouse. Just use "click"? Of course, "click" will only work if you have properly hooked up an on click event to the "AddContentBTN" div. Double check that clicking on it manually works.
  • Insane
    Insane over 10 years
    I used what you suggested above. driver.findElement...... Due to internal server restrictions, I was getting "no such element found" error. So, I thought to refresh the page before any actions and used - driver.navigate().refresh(); driver.findElement..... and BINGO!
  • Ben Smith
    Ben Smith over 10 years
    @Insane glad to hear it's now working! As my answer worked I.e. allowed yo to find element, I'd be grateful if you accepted the answer.