C# Selenium: Click button under iframe

2019-07-14 06:52发布

问题:

I have a iframe modal under my site. I am trying to click button in it but I am unable to do so. Below is my code. Please let me know what am i missing

driver.SwitchTo().Frame(driver.FindElement(By.Id("iframeid='frame_name'"))); 
driver.FindElement(By.Id("sendReuqest")).Click();

Expected Result: Button id: sendRequest should get clicked which is on iframe

Actual Result: Element is not found.

Please let me know if you have any questions.

回答1:

Try to do it this way. Let's take frame_name id as iframe_1. Whatever you frame id is you can add instead of iframe_1. Also you have a spelling mistake (typo) it might be sendRequest so I am adding as id of your button.

driver.SwitchTo().Frame(driver.FindElement("iframe_1"))); 
driver.FindElement(By.Id("sendRequest")).Click();

Hope it works. Please do comment and let us know.

Best of luck.



回答2:

It looks like you're trying to use By.Id() when you should be using By.CssSelector(). By.Id() expects you to pass a parameter which matches the ID element in the HTML.

driver.SwitchTo().Frame(driver.FindElement(By.CssSelector("[iframeid='frame_name']"))); 
driver.FindElement(By.Id("sendReuqest")).Click();


回答3:

Try that one:

driver.SwitchTo().Frame("frame_name"); 
driver.FindElement(By.Id("sendReuqest")).Click();