0

How can I build the xpath for Try it button from this website: https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert

driver.findElement(By.xpath("//div[@id='iframewrapper']/iframe/????")).click();

Also I tried this xpath //body[@contenteditable='false']/button which I know that isn't recommended. In Chrome console apear ok, but in Selenium doesn't find it and I don't know why.

enter image description here

JeffC
  • 22,180
  • 5
  • 32
  • 55
BOB
  • 700
  • 2
  • 16
  • 35
  • 1
    You should add a tag for the programming language you want. – JeffC Aug 20 '18 at 00:39
  • 2
    Possible duplicate of [How to handle iframe in Selenium WebDriver using java](https://stackoverflow.com/questions/9942928/how-to-handle-iframe-in-selenium-webdriver-using-java) – JeffC Aug 20 '18 at 00:39

2 Answers2

4

Write this code, First you need to switch to Frame and then you need to click that button

driver.switchTo().frame("iframeResult")    
driver.findElement(xpath: "//button[text()='Try it']").click()
JeffC
  • 22,180
  • 5
  • 32
  • 55
Rajagopalan
  • 5,465
  • 2
  • 11
  • 29
  • It works with: `driver.switchTo().frame("iframeResult"); driver.findElement(By.xpath("//button[text()='Try it']")).click();` Thank you ! – BOB Aug 19 '18 at 18:16
  • Also I don't understand why I need to use switchTo() method before click() method – BOB Aug 19 '18 at 18:26
  • 2
    @BOB Iframe is equal to HTML within another HTML so it has its own scope so whenever you find your element is in Iframe you need to switch to that Iframe before you locate any element. – Rajagopalan Aug 19 '18 at 18:43
  • @Rajagopalan for completion purposes, is it necessary afterwards to "switch back" (assuming that we are still in the same page and we want now to get an element outside of the frame)? And if so, how to do that? – danielcaballero88 May 19 '21 at 13:34
  • @khawabonga I did not understand your question , better you post another question. – Rajagopalan Jun 04 '21 at 14:15
  • @Rajagopalan let me give here the answer to my question, so it may help someone else: if after working with elements inside the frame, one needs to do something with elements outside the frame, one can do `driver.switchTo().defaultContent();` – danielcaballero88 Jun 04 '21 at 16:10
  • 1
    @khawabonga oh yes, you have to switch back. I confused because you used the word "outside" – Rajagopalan Jun 04 '21 at 16:14
4

In case if you want to use WebDriverWait :

WebDriverWait wait = new WebDriverWait(driver, 20)  
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")));  
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Try it') and @onclick='myFunction()']"))).click();
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • @Rajagopalan : Well , there is no harm if we use it. Execution time would remain same. – cruisepandey Aug 19 '18 at 18:08
  • Execution time doesn't remain same because you are checking whether element is enabled before you click! Checking for whether element is enabled takes time. – Rajagopalan Aug 19 '18 at 18:09
  • @Rajagopalan : That's hardly 500ms , even less if element is found immediately. Plus you will have stability. – cruisepandey Aug 19 '18 at 18:10
  • Yes, but I want to tell you execution time doesn't remain same! I agree that's negligible. – Rajagopalan Aug 19 '18 at 18:11
  • 1
    @Rajagopalan No, the execution time *does* remain exactly the same. If a wait is not necessary, then the initial check that the wait does will succeed... thus the execution time is exactly the same. If it's not exactly the same, then the wait had to wait and the find without a wait will fail. Adding a wait adds no time to the execution time if the element is already present. – JeffC Aug 20 '18 at 00:36
  • 1
    @Rajagopalan Further more, if the page ever does need a brief pause because it loads slowly, this method is safe where no wait will fail. This will avoid unnecessary intermittent failures. – JeffC Aug 20 '18 at 00:37