I'm making an app (console application) similar to a search engine. The app asks the user for input, then it googles that input and gathers the URLs of the first page of Google results. Then, it asks the user which link does he want to open, for example: "Which link should I open?" and the user should type "First", "Second", "Third", etc. I have managed to get to the point where I gather all URLs, but I don't know how to code the part where I choose which URL I want it to open.
Here's the code:
Console.WriteLine("Search for:"); //User Input
string command = Console.ReadLine();
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com"); //Opens Firefox, Goes to Google.
driver.Manage().Window.Maximize();
IWebElement searchInput = driver.FindElement(By.Id("lst-ib"));
searchInput.SendKeys(command); //Types User Input in "Search"
searchInput.SendKeys(Keys.Enter); //Hits "Enter"
//Gets Urls
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1));
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
using OpenQA.Selenium.Firefox...
var driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.com");
string text = ...
IWebElement webElement = ...
After opening, the user does some manipulations, enters text, and presses a button.
Is it possible to track these actions using Selenium???
Maybe there are some other tools?
I'm working in project where I have to buy a product from some website. I'll get a mail in Gmail I have to click on Received Email (Unread Mail) and interact with the clicked element.
So far I have bought the product and now I'm stuck with the Gmail; I'm not able to open the unread mail and interact with the element when I click 'Unread Mail'.
here is my code
driver.get("https://www.gmail.com");
driver.manage().window().maximize();
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);
driver.findElement(By.id("Email")).sendKeys("your mail");
driver.findElement(By.xpath(".//*[#id='next']")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement ele4=driver.findElement(By.xpath("//*[#id='Passwd']"));
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated((By.xpath("//*[#id='Passwd']"))));
ele4.sendKeys("yourpassword");
driver.findElement(By.xpath("//*[#id='signIn']")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
List<WebElement> unreademeil = driver.findElements(By.xpath("//*[#id=':3d']"));
String MyMailer = "StrapUI";
for(int i=0;i<unreademeil.size();i++)
{
if(unreademeil.get(i).isDisplayed()==true)
{
if(unreademeil.get(i).getText().equals(MyMailer))
{
System.out.println("Yes we have got mail form " + MyMailer);
break;
}
else
{
System.out.println("No mail form " + MyMailer);
}
}
}
driver.findElement(By.xpath("//*[#id=':3d']")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(10000);
driver.switchTo().frame(0);
Thread.sleep(5000);
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)", "");
Thread.sleep(3000);
}
}
Why do you want to interact with the email using Selenium? Unless you're creating automated tests for Google there shouldn't be a reason to do this with Selenium. The reason for this is that every little change Google makes to Gmail has a chance to break your script and requires modification.
Instead I'd recommend using an 'IMAP' library to help you with this. You can login and get the new messages. Using this you can check whatever it is you want to check in your email.
For example: I was creating integration tests for a company I used to work at. When I used a certain feature an email would be send containing a url. To verify this worked correctly I had to get this email and find the url in it. Next I used Selenium to get the url that was in the email and verified if it redirected me where I expected. I collected the email using imaplib for Python. I logged in, collected my INBOX and fetched the unread messages.
I have been having troubles with Selenium on and off for quite a while now. So, I thought I would create a nice simple example and hopefully a selenium expert can show me:
what I am doing wrong; or
what is wrong with the website that I am driving.
For this exercise, I have created a simple console application with a view to logging in to a library website. Obviously, username and password are not correct. But the fact is, the code which I present below cannot even type characters in a text box which is visible on the page:
static void Main(string[] args)
{
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(#"E:\");
var chromeOptions = new ChromeOptions();
chromeDriverService.Port = 7788;
var driver = new ChromeDriver(chromeDriverService, chromeOptions);
var nav = driver.Navigate();
nav.GoToUrl("http://sapln.ent.sirsidynix.net.au/client/charlessturt/");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("#libInfoContainer > div.loginLink > a"))).Click();
wait.Until(ExpectedConditions.ElementExists(By.Id("j_username"))).SendKeys("gfhjdskaf");
wait.Until(ExpectedConditions.ElementExists(By.Id("j_password"))).SendKeys("gfhdsjffd");
driver.FindElementById("submit_0").Click();
Console.ReadKey();
}
By all means, try and populate that text box. I would love to hear how you achieve it.
I am using:
v.2.43.1 of Selenium with the latest version of Chrome
v.2.43 of ChromeDriver
your dialog is opening in an iframe. You'll need to switch to that iframe before you can interact with the login form elements.
I didn't test your code, but you can follow the example below, by removing my comments:
static void Main(string[] args)
{
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService(#"E:\");
var chromeOptions = new ChromeOptions();
chromeDriverService.Port = 7788;
var driver = new ChromeDriver(chromeDriverService, chromeOptions);
var nav = driver.Navigate();
nav.GoToUrl("http://sapln.ent.sirsidynix.net.au/client/charlessturt/");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementExists(By.CssSelector("#libInfoContainer > div.loginLink > a"))).Click();
assuming your iframe is the first found you can use something like...:
driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0)));
Since your iframe doesn't have an id, we can't simply do:
driver.switchTo().frame("id of frame");
It would be super convenient if you could give your iframe an id.
Now lets continue with your code:
wait.Until(ExpectedConditions.ElementExists(By.Id("j_username"))).SendKeys("gfhjdskaf");
wait.Until(ExpectedConditions.ElementExists(By.Id("j_password"))).SendKeys("gfhdsjffd");
driver.FindElementById("submit_0").Click();
Once you're logged in, you need to switch back to the default content
driver.switchTo().defaultContent();
Console.ReadKey();
}
Simply inject that iframe switch before you attempt to interact with dialog, and inject the defaultContent() switch at the end before attempting to do anything else with initial page and you're good.
Good luck!
I'm new to automation testing. I'm using twitter as a test website to learn selenium.
I want to create a new tweet and for doing that I'm using the below scenario. I'm able to open the 'New tweet' pop-up but still can't enter text in its text box. Please help.
Pre-Requisite: Login into Twitter
Click on the button (for creating new tweet) on top right side of screen.
The pop up opens up
Click on the text box present on pop-up to enter text.
The cursor gets highlighted in text box and save button enables
Enter some text
Click on Save button
For me it's working till point 2 and after that it doesn't enter text in textbox
See my code below:
String popUpHandle = driver.getWindowHandle();
driver.switchTo().window(popUpHandle);
driver.findElement(By.id("global-new-tweet-button")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).click();
driver.findElement(By.xpath("(//button[#type='button'])[123]")).sendKeys("test tweet");
driver.findElement(By.xpath("//button[#type='button'])[106]")).click();
#user3241182
I just tested this myself. You do not need to use any window handling.
Code Below is a provided solution and works great.
WebDriver driver = new FirefoxDriver();
driver.get("https://twitter.com/");
WebElement username = driver.findElement(By.id("signin-email"));
username.clear();
username.sendKeys("your_twitter_handle");
WebElement pass = driver.findElement(By.id("signin-password"));
pass.clear();
pass.sendKeys("your_twitter_passwd");
WebElement signInButton = driver.findElement(By.xpath("/html/body/div/div[2]/div/div[2]/div[2]/div[2]/form/table/tbody/tr/td[2]/button"));
signInButton.click();
WebElement tweetButton = driver.findElement(By.id("global-new-tweet-button"));
tweetButton.click();
WebElement tweetBox = driver.findElement(By.id("tweet-box-global"));
tweetBox.clear();
tweetBox.sendKeys("This is an automated tweet!");
WebElement tweetSubmit = driver.findElement(By.xpath("/html/body/div[9]/div[2]/div[2]/div[2]/form/div[2]/div[2]/button"));
tweetSubmit.click();
I'm looking at a few pointers /a tutorial or maybe even another question to help me with my task. I'm looking to automate a web admin task. What I would like to do is :
Login to an application.
Navigate to a particular menu.
Search for a particular item through a search bar.
If the item is displayed in the search items then click on a set of buttons on the UI and proceed with the task.
If the item is not displayed in the search results then continue searching till the item is displayed , and then perform step 4.
I have been able to perform up to step 3 using the selenium IDE plugin for Firefox. But I'm not quite sure how to proceed and where to incorporate the logic for steps 4 and 5. Do I use a programming language?(If yes, then how?)
You hit the limits of the IDE pretty quickly. The IDE doesn't incorporate logic, but it is good for quick and dirty automation tasks, figuring out locator id's, and helping people learn the basics of selenium. I would suggest checking out learning how to script in Selenium Webdriver. Documentation: http://seleniumhq.org/docs/03_webdriver.html
So for example if you're using Java (this was stolen from the documentation):
public class Selenium2Example {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new FirefoxDriver();
// And now use this to visit Google
driver.get("http://www.google.com");;
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
//Pseudocode
if(element.isDisplayed()){
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
}
else{
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
}
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}