简体   繁体   中英

How to click on a button in a Alert?

i have to automate this page using java ,and selenium . I must click on the link which has label terms and conditions it shows up a box, and then i need to navigate it down, and click on Agree button.

在此处输入图片说明

I have already tried:

        driver.click("//*[@id=\"field_terms\"]/td[2]/div/div/label[1]/a");  // Click on the link

it opens the box for me, but i stuck after it.

I have 2 problems:

  1. How to scrol down the pop up?
  2. How to click on the Agree button?

Update:

Regarding the scroll problem i used the below method which does not work:

public void scrollDown() {
        JavascriptExecutor jsx = (JavascriptExecutor)driver;
        jsx.executeScript("arguments[0].scrollTop = arguments[1];","window.scrollBy(0,450)", "");
    }

Try using the CSS Selector to locate the I Agree button

'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'

It worked in my system. I am not a Java person, this is the code I wrote in python for your reference

driver = Chrome()
driver.get('https://signup.insly.com/signup')

terms_and_conditions = driver.find_element_by_link_text('terms and conditions')
terms_and_conditions.click()

import time
time.sleep(2)

i_agree = driver.find_element_by_css_selector(
              'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
          )
i_agree.click()

Let's start with the scroll: just use JavaScript...

JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");

Or you can just scroll up to the element needed...

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);

For the click 'I agree' use XPATH like this: "//*[text()='I AGREE']" then just preform click()

Hope this helps you!

Here is the code in Java [Tested & worked].

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/drivers/chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://signup.insly.com/signup");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.findElement(By.linkText("terms and conditions")).click();
    //Click the I AGREE button
    WebElement btnIagree = driver
            .findElement(By.xpath("//button[@btnclass='primary' and contains(text(),'I agree')]"));
    btnIagree.click();
    //Verify the check box for T&C is checked
    WebElement chkbxTnC=driver.findElement(By.xpath("//*[@id='agree_termsandconditions']/../span[@class='icon-check-empty icon-check']"));
    Assert.assertTrue(chkbxTnC.isDisplayed());

The problem is, as soon as you click on the T&C link, it takes few seconds to load the page and since a wait is needed before hitting the I AGREE Button. Apparently, the I AGREE Button is enabled and clickable without the need of scrolling down, so scrolling is not needed here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM