I'm trying to automate a simple search using selenium webdriver. My code is to launch the google.co.uk page in IE, wait until the search box has appeared, locate the search box and input "compare the market". However I keep getting a NoSuchElementException from the application, even though I can see it on the page.
My code:
class Program
{
static void Main(string[] args)
{
string searchEngine = "www.google.co.uk";
IWebDriver IEbrowser = new InternetExplorerDriver(#"C:\Drivers");
IEbrowser.Url = searchEngine;
WebDriverWait wait = new WebDriverWait(IEbrowser, TimeSpan.FromSeconds(25));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("q"));
});
var searchBox = IEbrowser.FindElement(By.Id("lst-ib"));
searchBox.SendKeys("compare the market");
}
}
Error Message:
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to find element with name == q
HTML info of the search box:
I get the same error message if I try to use any of the other page element locators like Id. But this only appears to error out using the google search page.
You've kinda overengineered this task. :) The code below works fine.
string searchEngine = "www.google.co.uk";
IWebDriver IEbrowser = InternetExplorerDriver(#"C:\Drivers");
IEbrowser.Navigate().GoToUrl(searchEngine);
IEbrowser.FindElement(By.Id("lst-ib")).SendKeys("compare the market");
If you want to wait for an element, there is a simpler way to do this using ExpectedConditions. See an example below. Read the linked docs to see the other available conditions that can be used.
WebDriverWait wait = new WebDriverWait(IEbrowser, TimeSpan.FromSeconds(25));
wait.Until(ExpectedConditions.ElementExists(By.Name("q")));
Related
(Previous thread)
(Thread for the same problem)
I want to extract information from a website, but in order to do that I need to login. Basically I can't authenticate with a HTTP/Web/Server request so I have to do it via browser, using WebDriver Selenium (Chrome). Now since the information can't be found through "view source-code", driver.PageSource doesn't work for example. Now when I do what has been suggested in the mentioned thread, I still get the wrong source code (there has also been the same problem reported in the comments). I need to get the code from the Inspector.
Any ideas? Thanks in advance!
class Program
{
static void Main(string[] args)
{
string url = "link";
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(url);
IWebElement email = driver.FindElement(By.Id("login-email-input-field"));
IWebElement password = driver.FindElement(By.Id("login-password-input"));
email.SendKeys("email");
password.SendKeys("password");
IWebElement login = driver.FindElement(By.Id("login-btn"));
login.Click();
driver.Navigate().GoToUrl(url);
Thread.Sleep(10000);
//Trying to get the inspect element code here
IWebElement element = driver.FindElement(By.Id("id"));
var html = element.GetAttribute("innerHTML");
Console.WriteLine(html);
}
}
}
I'm trying to determine if there's specific text on the page. I'm doing this:
public static void WaitForPageToLoad(this IWebDriver driver, string textOnPage)
{
var pageSource = driver.PageSource.ToLower();
var timeOut = 0;
while (timeOut < 60)
{
Thread.Sleep(1000);
if (pageSource.Contains(textOnPage.ToLower()))
{
timeOut = 60;
}
}
}
The problem is that the web driver's PageSource property isn't updated after the initial load. The page I'm navigating to loads a bunch of data via JS after the page has already loaded. I don't control the site, so I'm trying to figure out a method to get the updated HTML.
You are trying to solve the wrong problem. You need to wait for the text to appear using an XPath locator:
var wait = new WebDriverWait(driver);
var xpath = $"//*[contains(., '{textOnPage}')]";
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath));
Do you really need to search entire page?
I'll reference you to here: https://stackoverflow.com/a/41223770/1387701
with this code:
String Verifytext= driver.findElement(By.tagName("body")).getText().trim();
You can then check to see if the Verifytext contains the string you're checking for.
This works MUCH better if you can narrow the location of the text down to a particular webElement other than the body.
I have a context sensitive menu that needs text to be hightlighted in order for it to work. However, I'm having problems with selecting the text using Selenium. I start by finding the WebElement I'm looking for, before trying to interact with it using the different mouse events available.
When I'm trying to select parts of the text, it doesn't appear to do anything other than placing the marker at the end of the string.
This is what my textbox looks like:
This is what I need it to look like, or in other words, what I need Selenium to select (Just did it manually for the purpose of illustration:
This is along the lines of what I'm trying to do in code:
public static async Task HighlightElementByCssSelector(this RemoteWebDriver #this, string cssSelector, TimeSpan? timeout = null, TimeSpan? interval = null)
{
var element = await #this.FindElementByCssSelectorAsync(".testmarker-registryentryedit .testmarker-title-field");
Actions action = new Actions(#this).MoveToElement(element).ClickAndHold(element).MoveByOffset(10,0).Release();
action.Build().Perform();
}
#this represents the Driver in this case, and the FindElementByCssSelectorAsync being part of a 'wrapper-framework'.
I've tried different combinations of MoveToElement, DragAndDrop, ClickAndHold etc, but I just can't get it to work. Any pointers as to what might be wrong here?
According to what I understood about your problem I tried to solve it on my local machine (first day of vacation, lol). Sorry, I don't have VS on that machine so I wrote it in Java. The code should be self-explanatory:
#org.junit.Test
public void doTest(){
String query = "This is a test";
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
WebElement searchBox = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("lst-ib")));
searchBox.sendKeys(query);
// make sure it has focus
searchBox.click();
Actions actions = new Actions(driver);
// go to the beginning of input
actions.sendKeys(Keys.HOME).build().perform();
int length = query.substring(0, query.indexOf("a")).length();
actions.keyDown(Keys.LEFT_SHIFT);
for (int i = 0; i < length; i++){
actions.sendKeys(Keys.ARROW_RIGHT);
}
actions.keyUp(Keys.LEFT_SHIFT);
actions.build().perform();
}
Results in:
Is this what you wanted?
I am trying to get the URL of the second page of a yellowpages result with the following code:
var driverService = PhantomJSDriverService.CreateDefaultService();
var driver = new PhantomJSDriver(driverService);
driver.Navigate().GoToUrl(new Uri("http://www.yellowpages.com/los-angeles-ca/pizza?g=Los+Angeles%2C+CA"));
string url = driver.Url;
var next = driver.FindElementByCssSelector(".next");
next.Click();
string newUrl = driver.Url;
The "next" link is found and clicked but I do not get the new URL after calling next.Click().
Other pages work fine. I am only having problems on yellowpages right now.
Any ideas?
Try this for clicking on the web element instead of using click():
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", next);
Make sure you have turned on console output, so you could see the exact error:
service.HideCommandPromptWindow = true;
I had similar problem, and when i turned on console output I noticed the following error: "can't find variable: __doPostBack".
In my case, that was because of site declined defaut Phantom's user agent, so I had to change it (based on this answer).
Using webdriver 2.40.0 (installed from nugget packages) and writing code in C# I am
- opening a link for my company website which generates a certificate error page
- clicking the override link element to allow me to continue to the site
- clicking an enter button on that page which generates a pop up window that also had a certificate error page on top of it
My problem is when I try to select the pop up window a “noSuchWindowException” is thrown, code:
namespace webDriverDemo
{
class Program
{
static void Main(string[] args)
{
string setURL = "xxxxx";
IWebDriver driver = new InternetExplorerDriver(#"C:\Drivers");
driver.Url = setURL;
String loginPage = driver.CurrentWindowHandle;
var securityLine = driver.FindElement(By.Id("overridelink"));
if (!securityLine.Equals(null))
{
securityLine.Click();
}
var enterBtn = driver.FindElement(By.Id("EnterButton"));
enterBtn.Click();
//Select the pop up window
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");
driver.SwitchTo().Window("xxxx");
I’ve tried:
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()")
and
String riskPage = driver.CurrentWindowHandle;
and switching to that window, I’ve also tried
driver.SwitchTo().Window();
but I think the problem is that I can’t get to the window name of the certificate error page and cannot select and element on that page and try to save it as a separate handle. Really need help!
Once you have performed the action enterBtn.Click(); that launches the popup window, you need to then switch context to the new window (using it's window handle, not title) to be able to interact with it.
You can obtain the popup window's handle from the driver.WindowHandles list.
var riskPageHandle = driver.WindowHandles.FirstOrDefault(hwnd => hwnd != loginPageWindowHandle);
if(riskPageHandle ==null)
{
//popup not found, log error or handle
}
else
{
//switch to the popup
driver.SwitchTo().Window(riskPageHandle);
Console.WriteLine("Popup window title is : " + driver.Title);
//now accept the certificate error (your code, I haven't tried it)
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");
}