1

I'm using a library "Selenium" on Java to write a script to perform online tasks. It works perfectly fine on sites like facebook, youtube, etc. For some reason, in this website it does not: kingdoms.com . The button I want to click has this line of code:

<a data-mellon-iframe-url="/authentication/login" id="loginButton" data-selector="#mellonModal" class="jqFenster"><button> <span>Entrar</span></button></a>

The code I wrote for that is:

 driver.findElement(By.id("loginButton")).click(); 

And it does not click. But if I print this line:

     System.out.println(driver.findElement(By.id("loginButton")).getText()); 

it prints "Entrar", so the script knows the button but for some reason it won't click it.

Any idea? I've tried putting the script on sleep for 3s before clicking, for the case the button wasn't load on time, but it didn't fix it...

Pedro Lima
  • 387
  • 2
  • 14

1 Answers1

1

Actually the anchor tag is hidden behind button tag. You need to click the button which is inside anchor tag. I tried with css selector and it works

driver.findElement(By.cssSelector("#loginButton button").click()

Add wait for the button before clicking,

    driver.get("https://www.kingdoms.com/");
    WebDriverWait wait = new WebDriverWait(driver, 60);
    WebElement loginBtn= wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#loginButton button")));
    loginBtn.click();
Navarasu
  • 8,209
  • 2
  • 21
  • 32
  • I was using Thread.sleep() to wait. It does work!!! Thanks a lot, but where did you see that "#loginButton button" ? Since that the login box (the email and password) seem to have the same issue! – Pedro Lima Nov 02 '18 at 19:41
  • That is a different case. user name and password is present in iframe. I see it is iframe inside iframe. You need to switch to first iframe and then to next iframe. Check this post https://stackoverflow.com/questions/23302769/python-selenium-switch-into-an-iframe-within-an-iframe Then try to sendkeys to username and password – Navarasu Nov 02 '18 at 19:46
  • Im sorry, but this is not something that I am familiar with. Could you explain please, that post wasn't very enlightening for me. Why can't I access it by By.name("email") ? – Pedro Lima Nov 02 '18 at 19:50
  • Refer this blog to understand iframe https://www.guru99.com/handling-iframes-selenium.html – Navarasu Nov 02 '18 at 20:21