I'd like to make a simple console application, similar to a search engine.
It asks the user what to search, finds results on google, and then the user can type "Click on the first/second/third link(etc)" and the application automatically clicks on that link.
I've tried using Selenium and I got to the point where I could initiate the search and find the results, but I didn't know how to actually click on a link. I've also tried using mouse coordinates, but the code is too complicated for me.
I've also tried to find a way to extract the links of the first 5 google results and THEN by some way click them.
Anyway, I've been searching for hours, but I haven't found anything yet. It would be amazing if you guys could help me out!
Here's the code:
static void Main(string[] args)
{
Console.WriteLine("Search for:");
string command = Console.ReadLine();
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com");
driver.Manage().Window.Maximize();
IWebElement searchInput = driver.FindElement(By.Id("lst-ib"));
searchInput.SendKeys(command);
searchInput.SendKeys(Keys.Enter);
}
If it's a console app, you won't be able to click a link because the page won't be displayed. You could grab the links off the search results page but then how would you display the page after you navigate??? I don't guess I understand what the overall goal of your project is.
If all you want is to grab the links off the google search results page you can use the code below. It will grab the URLs of all of the search results on the page. You can then display how ever many of the links on the screen and then after the user chooses a link, navigate (something?) to the desired URL.
IWebElement searchInput = Driver.FindElement(By.Id("lst-ib"));
searchInput.SendKeys(command);
searchInput.SendKeys(Keys.Enter);
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(5));
By linkLocator = By.CssSelector("cite._Rm");
wait.Until(ExpectedConditions.ElementToBeClickable(linkLocator));
IReadOnlyCollection<IWebElement> links = Driver.FindElements(linkLocator);
foreach (IWebElement link in links)
{
Console.WriteLine(link.Text);
}
Related
im building roulette bot, im finding element by xpath i can get its class attiributes etc. but i cant click on it. its always clickable (bot finds ifrime and then finds element and clicks its working but this website gamescreen in iframe>iframe bot can find button but not clicking)
firefox.SwitchTo().Window(firefox.WindowHandles.Last());
var DBViFrame = firefox.FindElement(By.TagName("iframe"));
firefox.SwitchTo().Frame(DBViFrame);
DBViFrame = firefox.FindElement(By.TagName("iframe"));
firefox.SwitchTo().Frame(DBViFrame);
var dataEntryButton = firefox.FindElement(By.XPath("//*[name()=\"svg\"]//*[name()=\"g\"]//*[name()=\"rect\"][" + num + "]"));
dataEntryButton.Click();
Is firefox your WebDriver? If so, maybe try execute_script and see if that works?
firefox.execute_script("arguments[0].click();", dataEntryButton)
I am trying to automate the scheduling of pinning items to Pinterest using Tailwindapp.com. I am using a console app in .NET (C#) with Selenium Chromedriver. I start up the browser and enable the tailwind extension and login to tailwind. Then I go to the site I am trying to pin images from, get to the product page, search for the button and attempt to click it. That's where it falls apart. The 'Schedule' button in Tailwind appears over all images on the page as you hover. When I do an XPath search, it only returns a single button for the whole page (the console line below shows 1).
public static void ClickScheduleButton(IWebDriver driver) {
// get all the buttons and then use the first one
IList<IWebElement> buttons = driver.FindElements(By.XPath("//*[#id='tw_schedule_btn']"));
Console.WriteLine("Number of items found: " + buttons.Count());
IWebElement scheduleButton = buttons.ElementAt(0);
Actions actions = new Actions(driver);
actions.MoveToElement(scheduleButton).Click().Perform();
}
On the perform method, I get the following error: OpenQA.Selenium.WebDriverException: 'javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
From what I've read, this seems to mean that there are more than 1 element but I seem to have ruled this out with the number of buttons found. I have attempted to do a wait to make sure it's available but I do not believe that is the issue.
I have tried to find an example just trying to do this with a Pinterest button because in theory it would be the same logic but I cannot find anything for that either.
My assumption is that it's a problem just getting the button to appear on the correct image? But that's just a guess.
If I'm understanding this correctly you want to click the button who appears in when you hoover the mouse in the image.
If you can hoover the mouse and wait until the expected xpath appears should solve it.
protected virtual void HooverMouse(By element)
{
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(element)).Build().Perform();
}
The element will be the first image you have, so you must change your driver.findelements("the button") to the "images" and loop arround this.
In pseudo code will be:
Hoover(image) -> WaitButtonAppears -> ClickButton
I am trying to automate login of following site:
https://www.sbs.com.au/ondemand/
after pressing Signin/Signup its opening login modal which is an iframe.
I am trying to locate email,password, login button with following code but not working. Its opening modal but after that doing nothing just test failing but no error message either.
Code for clicking Signin/signup link and switch to iframe:
var link = Driver.Instance.FindElement(By.Id("capture_signin_link"));
link.Click();
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(10);
Driver.Instance.SwitchTo().Frame(1);
Code for passing value in email,password,and clicking login button:
var inputus = Driver.Instance.FindElement(By.Name("email"));
inputus.SendKeys(username);
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(5);
var inputpass = Driver.Instance.FindElement(By.Name("password"));
inputpass.SendKeys(password);
Driver.Instance.Manage().Timeouts().ImplicitWait.Seconds.Equals(5);
var loginbutton = Driver.Instance.FindElement(By.ClassName("traditional-login"));
loginbutton.Click();
I am not sure if I am passing correct value to find locators and ifame. Please visit above site information for DOM info.
Looking forward a good solution. Please help me
My Latest Code for clicking signup to open login modal:
var link = Driver.Instance.FindElement(By.Id("capture_signin_link"));
link.Click();
Driver.Instance.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10.00);
Driver.Instance.SwitchTo().Frame(Driver.Instance.FindElement(By.Id("login-iframe")));
Driver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
Code for login:
IWebElement inputus = Driver.Instance.FindElement(By.XPath(".//*[#id='onboarding']/div[2]/form/section[2]/div/div[1]/input"));
inputus.SendKeys(username);
Driver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10.00);
var inputpass = Driver.Instance.FindElement(By.Name("password"));
inputpass.SendKeys(password);
Driver.Instance.Manage().Timeouts().ImplicitWait=TimeSpan.FromSeconds(10.00);
var loginbutton = Driver.Instance.FindElement(By.ClassName("traditional-login"));
loginbutton.Click();
Above code worked for Chrome. On firefox its showing "Array out of index error" on Sendkeys() method. I found on some blog a lot of people are facing same problem and suggesting to downgrade firefox browser version to get proper support from getco driver as getco driver failing to handle sendkeys().
I haven't try above suggestion. Will try and update here. Till then if you have any other suggestion on that plz let me know.
I'm trying to build my first test with selenium and got a problem.
I'm searching for a element, no problem. I can click on it, get the
text in the element... every thing works fine.
But double click on the element just doesn't work. Selenium
clicks in the wrong location. I made a screenshot of this situation:
Screenshot
To find the row i use xpath and search for the text in the cell, but this text is unique(I checked it)
private readonly string _identityPath = ".//td[.= 'All Employees']";
...
mainPage.FindElement(By.XPath(_identityPath)).Click(); //Works(dotted box)
Actions builder = new Actions(mainPage);
IAction doubleClick = builder.DoubleClick(mainPage.FindElement(By.XPath(_identityPath))).Build();
doubleClick.Perform(); //wrong location/element
/*
Actions action = new Actions(mainPage);
action.DoubleClick(mainPage.FindElement(By.XPath(_identityPath)));
action.Perform(); *///wrong location/element
This page is in an iframe and the grid is a dojo component... maybe the problem
comes from there. Any ideas whats wrong? I have no idea where this is coming from. :/
Greets
It is common issue that Actions builder does not work.
Using JavaScript should help - find answer in this thread:
Selenium 2/Webdriver - how to double click a table row (which opens a new window)
If the element is in an iframe, you need to switch to that iframe in order to interact with the element.
From the WatiN website:
// Open a new Internet Explorer window and
// goto the google website.
IE ie = new IE("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
// Uncomment the following line if you want to close
// Internet Explorer and the console window immediately.
//ie.Close();
The above sample works just fine. However, since I do not want to open up a browser window, I modified the above code to use MsHtmlBrowser:
// goto the google website.
var ie = new MsHtmlBrowser();
ie.GoTo("http://www.google.com");
// Find the search text field and type Watin in it.
ie.TextField(Find.ByName("q")).TypeText("WatiN");
// Click the Google search button.
ie.Button(Find.ByValue("Google Search")).Click();
The TypeText line is throwing an exception. Any idea what's wrong?
The MsHtmlBrowser is only there to find elements and read their attribute values. There is no support for clicking a link, typing text, firing events, no session state or any other way of interact like with a normal browser. So us it for scrapping only.
HTH,
Jeroen