Selenium click doesn't wait webpage load in firefox - c#

When I try to use the selenium click in firefox, it doesn't wait till the element load completes.
Has anybody faced a similar problem?

First you should define default timeout of Webdriver after initialization as following :
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
After that you can wait that web element before clicking it like following :
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By locator));

Related

Selenium WebdriverTimeoutException although the page is already loaded

I'm new to Selenium and try to automatically open a website in Full Screen mode.
The website has a Login which is already working with Selenium.
After the login only one button has to be pressed.
Hereby an WebdriverTimoutException is thrown in the second last line.
The InnerException says NoSuchElementException.
But when I open the web console, I can see the button.
IWebDriver driver = new EdgeDriver(System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString() + "\\webdriver");
driver.Navigate().GoToUrl(#"http://examplehomepage.com");
driver.FindElement(By.Id("username")).SendKeys("abc");
driver.FindElement(By.Id("password")).SendKeys("password123");
driver.FindElement(By.TagName("button")).Click();
driver.Manage().Window.FullScreen();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.TagName("button")));
element.Click();
I tried it with:
Edge (85.0.564.44)
Chrome (85.0.4183.83)
Firefox(80.0.1).
You can use sleep to delay the interaction
You are using ElementToBeClickable. You should also use ElementToBeVisible and also check whether element is enabled as well from isEnabled function.

"unknown error: cannot read property 'scrollleft' of null" in Chrome using selenium

In Chrome when I am clicking button, I am getting "unknown error:
cannot read property 'scrollleft' of null
Their is 1 Input field in that page I am able to enter value in that only Button click is not working.
This Click is working fine in Firefox. issue only in Chrome
IWebElement element = wait.Until(d => d.FindElement(locator));
wait.Until(ExpectedConditions.ElementToBeClickable(element));
element.Click();
Note:
There is no frames in this page, no scrolls.
You need to invoke Click() once the element is returned through WebDriverWait as follows:
IWebElement element = wait.Until(d => d.FindElement(locator));
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(element)).Click();

select radio button in selenium c#

I'm new to XPath and CssSelector.
below is the target html source.
<input value="1" name="uji.model.611876.button" type="radio"></input>
611876 is a random number.
I tried with the code:
driver.FindElement(By.Id("//input[#value=\"1\"]")).Click();
and
driver.FindElement(By.Id("//input[#value='1']")).Click();
but the Unable to locate element error occurred.
I need help for that situation. Thank you for reading.
If you get ElementNotVisibleException try to wait some time until target input become visible:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[starts-with(#name, \"uji.model.\")][#type=\"radio\"]")));
element.Click();
You can do something like
driver.findElement(By.tagName("input")).Click();
You can try JavascriptExecuter to execute javascript code if it is causing problem -
IWebElement element= driver.FindElement(By.XPath("//input[#value=\"1\"]")));
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("arguments[0].click();", element);

why the modal window doesn't show up when clicked by selenium

I try to click on the login button(Đăng nhập) to show up the login box, but fail to achieve it.
The loginbox just doesn't show up.
Selenium, webdriver are all latest version
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
// driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
// driver.FindElement(By.XPath("//a[#href='#loginform']//span")).Click();
// driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[#href='#loginform']//span"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_username")));
wait.Until(ExpectedConditions.ElementExists(By.Id("navbar_password")));
// var loginBox= wait.Until(ElementIsClickable(By.Id("loginform"))); >> fail
driver.Scripts().ExecuteScript("document.getElementById('navbar_username').style.display='inline';");
driver.Scripts().ExecuteScript("document.getElementById('navbar_password').style.display='inline';");
Console.ReadKey();
}
C# extension:
public static IJavaScriptExecutor Scripts(this IWebDriver driver)
{
return (IJavaScriptExecutor)driver;
}
There are 2 problems.
1- There's a webpage coming up prior to the actual forum page, when you are navigating to the site. Below is the image for that:
So you have to click on the button, that is highlighted above first. And, then after you will be able to navigate to the forum's page.
2- Your button is certainly getting clicked, but since the webpage has not properly loaded, the click action is not proceeding.
Hence, you need to wait for certain element that gets loaded when the page is loaded properly.
Below code will help you out:-
using (IWebDriver driver = new ChromeDriver())
{
driver.Navigate().GoToUrl("http://sinhvienit.net/forum/");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30)); //Give the implicit wait time
driver.FindElement(By.XPath("//button[#id='btnSubmit1']")).Click();// Clicking on the button present in prior page of forum
//Waiting till the element that marks the page is loaded properly, is visible
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ExpectedConditions.ElementIsVisible(By.XPath("//*[#id='vtlai_topx']/a")));
driver.FindElement(By.XPath("//a[#href='#loginform']")).Click();
...
You can proceed with rest then.

How to select an option from the Telerik RadComboBox using Selenium WebDriver and c#?

I am trying to select an option in a Telerik RadComboBox Asp.Net control using Selenium WebDriver and I can't make it work consistently. I am using the following code
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(RadComboBoxArrowBy));
RadComboBoxArrow.Click();
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//input[contains(#id, 'rcbRadComboBox_Input')]")));
RadComboBox.SendKeys(division);
RadComboBox.SendKeys(Keys.Tab);
where RadComboBoxArrow is the arrow at the right of the input element in the Telerik RadComboBox.
I am using this code in multiple places and only in one it fails from time to time with
System.TimeoutException: Timed out after 30 seconds --->
OpenQA.Selenium.NoSuchElementException: Unable to locate element:
{"method":"xpath","selector":"//input[contains(#id, 'rcbRadComboBox_Input')]"}
Before suggesting the use of SelectElement please consider the fact that Telerik RadComboBox doesn't render a select tag.
I use driver.Click()
First I click the combo to cause it to open:
driver.Click(By.Id("ctl00_WCEContentPlaceHolder_RadToolbar_ReportsMenu_rttb1_ctl00_rcb_Reports");
Then I click the element by ID. This one happens to be the first in the list.
driver.Click(By.Id("ctl00_WCEContentPlaceHolder_RadToolbar_ReportsMenu_rttb1_ctl00_rcb_Reports_c1");

Categories

Resources