I can't figure out how to convert this code in the example text. How should I write it?
wait.Until(e => e.FindElement(By.XPath("//a/h3")));
Original Site : Wait | Selenium
Original Code :
driver = new ChromeDriver();
driver.Url = "https://www.google.com/ncr";
driver.FindElement(By.Name("q")).SendKeys("cheese" + Keys.Enter);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement firstResult = wait.Until(e => e.FindElement(By.XPath("//a/h3")));
Console.WriteLine(firstResult.Text);
Ps. I would like to know the following as well
var foo = new WebDriverWait(driver, TimeSpan.FromSeconds(3))
.Until(drv => drv.FindElement(By.Name("q")));
.Until:
■ Function DfaultWait(Of IwebDriver).Until(Of TResult)(condition As Func(Of IWebDriver, TResult))As TResult (+ 1 overload)
Thank in Advance
ーーーーーーーーーーー
▼ I am glad to inform you that the problem is now solved.
Dim firstResult As IWebElement = Wait.Untile(Function(e) e.findElement(By.XPath("//a/h3")))
Thanks for replying.
This
wait.Until(e => e.FindElement(By.XPath("//a/h3")));
is this
wait.Until(Function(e) e.FindElement(By.XPath("//a/h3")))
Related
Is there a way to perform a wait.Until by somehow searching the element from another element, rather than from the whole driver?
example:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSec));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(by));
I cannot modify the 'by' to be too specific and when it is searched under the driver and gets the wrong element.
IWebElement element = wait.Until(drv => drv.FindElement(by));
this option also searches under the driver.
I want something like this:
public static IWebElement WaitElement(this IWebElement webElement, IWebDriver driver, By by, int timeoutInSec = 5)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSec));
IWebElement element = wait.Until(webElement.FindElement(by));
}
When I try to write this code I get this error:
enter image description here
It may be late, but hopefully will help someone else. Just change the syntax of your last line in the 3rd option to the following:
IWebElement element = wait.Until(d => webElement.FindElement(by));
I'm trying to create an automated ui test with selenium in c#. Here is my code:
driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
driver.Navigate().GoToUrl("my_url");
driver.FindElementById("textBox").Clear();
driver.FindElementById("textBox").SendKeys("tire");
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until((ExpectedConditions.ElementIsVisible(By.Id("Moto"))));
driver.FindElementById("Moto").Click();
Before using a wait.until, I was getting the exception ElementNotVisibleException, but now I'm getting the exception WebDriverTimeoutException because the element with the id "Moto" is not visible.
Here is a screenshot of a part of the DOM:
So why the moto checkbox is not found or is not visible?
Try the below code (in Java) as it is working at my end for the same structure -
driver.findElement(By.xpath("//label[#for='Moto']")).click();
I don't know why it is causing problem to find <input> tag by id
You might need to scroll to the element to make it visible
IWebElement moto = driver.FindElement(By.Id("Moto"));
Actions actions = new Actions(driver);
actions.MoveToElement(moto).Perform();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Moto"))).Click();
driver.FindElement(By.XPath("//textarea[#ng-model='vm.system.systemdescription']")).SendKeys("abc"); //is able to find element.
Whereas if do same thing using ngDriver i'm getting
> async timeout exception or javascript Invalid operation exception
ngDriver.FindElement(NgBy.Model("vm.system.systemdescription")).SendKeys("jkgf");
Also tried the following but doesn't help
ngDriver.FindElement(NgByModel.Name("vm.system.systemdescription")).SendKeys("jkgf"); also does not work.
Here is d code snippet private IWebDriver driver = new InternetExplorerDriver();
private NgWebDriver ngDriver;
private WebDriverWait wait;
private Actions actions;
private int wait_seconds = 30;
[SetUp]
public void InitializeBrowserToSearchEngine()
{ ngDriver = new NgWebDriver(driver);
driver.Navigate().GoToUrl("req_url");
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(60));
ngDriver = new NgWebDriver(driver);
wait = new WebDriverWait(driver, TimeSpan.FromSeconds(wait_seconds));
actions = new Actions(driver);
Console.WriteLine("Opened Browser with the given URL");
}
[Test]
[Order(6)]
public void OpenNewSystemConfig()
{
string url = "req_url";
ngDriver.Url = url;
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
WebDriverWait w1 = new WebDriverWait(ngDriver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id("Administration_1")));
driver.FindElement(By.Id("Administration_1")).Click();
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.Id("System_Configuration_0")));
driver.FindElement(By.Id("System_Configuration_0")).Click();
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//textarea[#ng-model='vm.system.systemdescription']")));
Thread.Sleep(5000);
driver.FindElement(By.XPath("//textarea[#ng-model='vm.system.systemdescription']")).SendKeys("ijfk");
//w1.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(NgBy.Model("vm.system.systemdescription")));
//ngDriver.FindElement(NgBy.Model("vm.system.systemdescription")).SendKeys("jkgf"); ;
wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(By.XPath("//select[#data-ng-model='vm.system.SystemTypeId']")));
driver.FindElement(By.XPath("//select[#data-ng-model='vm.system.SystemTypeId']"));
//wait.Until(ExpectedConditions.VisibilityOfAllElementsLocatedBy(NgBy.Model("vm.system.systemdescription")));
//ngDriver.FindElement(NgByModel.Name("vm.system.SystemTypeId"));
// ngDriver.FindElement(NgByModel.ClassName("form-control ng-pristine ng-untouched ng-valid ng-not-empty ng-valid-required"));
}
The commented code is not working. I need to use protractor to find the elements by ng-model and ng-binding. Since Xpath are not be used i need a way to find the element which does not have id and angular components could be find by the locators such as ng-model and ng-binding.
Also I have added protractor in the reference using nuget package manager
please help to resolve
just try element(by.model("vm.system.systemdescription")).sendKeys('jkgf);
You have a typo in your command sendKeys should start with small case 's' not 'S'
Hope this helps..
I have used the Selenium IDE to generate some code and I am now i want to put a wait command until the browser reloads after a button click. For example:
public void TheGetTest()
{
driver.Navigate().GoToUrl(baseURL + "/");
driver.FindElement(By.XPath("(//a[contains(text(),'Get Quotes')])[2]")).Click();
}
After the Click command, I want to see what URL is loaded. Is this possible or do I have to have to use some element on the page to look for?
WebDriverWait wait5 = new WebDriverWait(driver, 100);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>(d => d.FindElement(By.Id("someDynamicElement")));
How would this code be used if the dynamic element is the page itself?
Its been some time since i used selenium.
But maybe you can use a javascript function to determine if the document is in ready state again.
wait.Until(driver1 => ((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
I want to get the text Sample from this structure:
<td id="IDName">Sample</td>
so I tried this:
driver1.FindElement(By.Id("IDName")).Text;
but it always return null.
Is there any reason why is it not working?
It's Hard to say why .Text is not working in your case, Might be possible when you are going to find element it's present on the DOM without text, So you should WebDriverWait to wait until element exists as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.Text;
Or might be possible it's designing issues of your HTML, then you can get text by using .GetAttribute("innerHTML") as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.GetAttribute("innerHTML");
Or then you can get text by using .GetAttribute("textContent") as below :-
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementExists(By.Id("IDName")));
element.GetAttribute("textContent");
Hope it helps...:)