Selenium WebDriver: unable to locate element (C#) - c#

I'm trying to automate Paypal withdrawals by using C# and Selenium. The application logs into Paypal using provided credentials and clicks on the 'transfer money' link, which then shows a pop-up (which looks to be an iframe). My problem is that I can't click on any of the elements in the pop-up, and I've tried every suggestion I could find.
Here is a screenshot of the form and the underlying html:
paypal form
I'm trying to click on the 'From' dropdown and among other things I've tried:
driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();
and
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].hidden = false;", driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")));
but either get and 'Unable to locate element' or 'Element not visible' errors. How do I get to the 'From' input element on the pop-up? (If you're using paypal, you could also log in and take a peek at the pop-up if needed).

You need to switch to the iframe first
IWebElement frame = driver.FindElement(By.TagName("iframe")); // locate the iframe element
driver.SwitchTo().Frame(frame);
driver.FindElement(By.XPath("//*[#id=\"selection-container\"]/form/section/table/tbody/tr[2]/td/div[1]/div[1]")).Click();
And to switch back
driver.SwitchTo().DefaultContent();

Try
[FindsBy(How = How.CssSelector, Using = "div[class$='source-dropdown']")]
public IWebElement _ddSource;
The '$' specifies the end of the attribute, in the case the end of the class is source-dropdown

at first you need to switch to that iframe. use the below code:
IWebElement frame = driver.FindElement(By.CssSelector("iframe[src ='/moneytransfer']");
driver.SwitchTo().Frame(frame);
now you can click on that pop up by using this cssSelector:
div[class$='source-dropdown']

Related

Unable to click Sign in button of Microsoft Login in C# Selenium

Trying to click Sign In button but getting "Stale element Exception error".
IWebElement email = driver.FindElement(By.XPath("//*[contains(#name,'loginfmt')]"));
IWebElement password = driver.FindElement(By.XPath("//*[contains(#name,'passwd')]"));
IWebElement signIn = driver.FindElement(By.XPath("//*[contains(#id,'idSIButton9')]"));
IWebElement signInbtn = driver.FindElement(By.XPath("//*[#id='idSIButton9']"));
IWebElement signInbtn1 = driver.FindElement(By.XPath("//input[#type='submit']"));
email.SendKeys("automate#outlook.com");
email.SendKeys(Keys.Enter);
Thread.Sleep(1000);
password.SendKeys("Abc*123$");
password.SendKeys(Keys.Enter);
signInbtn1.Click();
Error:
OpenQA.Selenium.StaleElementReferenceException : stale element
reference: element is not attached to the page document
The reason of getting the StaleElementReferenceException is that the driver have found the element, but the page refreshed by the moment, you try to interract it, so element state is stale.
Try to initialize the element signInbtn1 after you fill the password field:
password.SendKeys("Abc*123$");
password.SendKeys(Keys.Enter);
IWebElement signInbtn1 = driver.FindElement(By.XPath("//input[#type='submit']"));
signInbtn1.Click();
I wasn't using C# for the test script, but I'm calling the selenium driver via javascript (within a jest test) and was getting the same problem for that specific Login Modal you mentioned.
Looking at the HTML in dev tools, I found that there is a subtle issue when using the ID ("idSIButton9") as the button element will change once the email has been entered in.
ie. The button element's value will change from 'Next' to "Sign in"
I found that using the id twice to identify the same button element results in the stale element issue.
So, for the second time round, I found the button element using the following xpath
"//input[#value='Sign in']"
This is much more specific than the id in this case.
Hope that helps.

How to click on the SIGN IN button using Selenium and C#

I am trying to create this simple test where you head to the URL, enter your login credentials and then click the button to sign in. It is doing everything, except for clicking the button. I am trying to doing it by calling up ClassName. Can anyone look at my test and see what I am doing wrong?
public void test_search()
{
var driver2 = new ChromeDriver(#"C:\Users\MyName\Desktop\NUnitTestProject1\NUnitTestProject1\bin\Debug\netcoreapp2.1");
driver2.Navigate().GoToUrl("https://portal.crushdata.com/");
driver2.FindElement(By.Name("Email")).SendKeys("email#email.com");
driver2.FindElement(By.Name("Password")).SendKeys("Password");
driver2.FindElement(By.ClassName("btn bg-teal btn-block btn-lg waves-effect")).Click();
}
This is my classname for my button.
Use CSS selector as shown below:
By.ClassName("btn.bg-teal.btn-block.btn-lg.waves-effect")
Each dot represents a class.
See this page for more info and here is an example from that page:
.name1.name2
Selects all elements with both name1 and name2 set within its class attribute
To click on the SIGN IN button you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.btn.bg-teal.btn-block.btn-lg.waves-effect"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[text()='SIGN IN']"))).Click();
Try making use of the button xpath.
Open the dev tools. Right click on the button you want to be clicked > Select Inspect >Then right click the html in the dev tools window and Copy Xpath from the Copy option.
Then in you code replace FindElement with FindElementByXPath:
driver2.FindElementByXPath("//*xpath/goes/here")).Click();
Given your shared html block, the following XPath will suffice.
//div[contains(#class = "text-center")]//button[contains(#class, 'btn bg-teal btn-block btn-lg waves-effect') and #type = 'submit']
If the driver is still unable to click you should consider the following:
Is the XPath unique? paste the xpath in chrome's devtools search box in the inspect element tab and make sure the provided xpath is targeting the element you are intending. If this is not the case then you should make the xpath more unique.
Is the element in an iframe? if the element is in an iframe the driver won't be able to locate it by default. In such cases you will need to first switch to the iframe and then attempt to locate and interact with the element.
Is the element clickable, visible and enabled? To check these properties first find the element and store in a separate variable and then check the said properties are true.

Click Dynamic button with Selenium (Pinterest/TailwindApp)

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

Automating 'To' from Contacts List Gmail

When you compose an e-mail in Gmail, to send the email to people on your Contacts list you have to click 'To' which leads you to a pop-up box. Is there a way to automate the closing of this box? I'm using Selenium and C#.
I think you're likely running into problems because that modular window is within an iFrame, and you need to tell Selenium to specifically look into that iFrame before searching.
See handling-iframes-using-selenium-webdriver.
Unfortunately, Google seems to assign a new ID to this iframe every time it's created, so you can't rely on that, so you'll need to create an IWebElement for the iframe element and pass it into the .SwithTo().Frame() call.
Specifically, you could try this (I tested this out, and it worked for me).
//Get iFrame element and switch to it.
IWebElement selectContactFrame= driver.FindElement(By.CssSelector("iframe.KA-JQ"));
driver.SwitchTo().Frame(selectContactFrame);
//Find cancel button and click it.
IWebElement cancelButton = driver.FindElement(By.XPath("//div[text()='Cancel']"));
cancelButton.Click();

Action on SharePoint New Item form in selenium 2 using C# is not working

I am trying to automate sharepoint site new item form but what ever method i try it is showing not found.
I tried switchTo() to a new iframe, window...
Tried this code which finds the outer content
IWebElement table1 = WebElement.FindElement(By.XPath("//table[#class=\"s4-wpTopTable\"]"));
int table1count = WebElement.FindElements(By.XPath("//table[#class=\"s4-wpTopTable\"]")).Count;
MessageBox.Show(table1count.ToString());
above code displays the table count as 2. Going beyond this element does not show any element.
And I am using IE as the browser.
I used Xpath and could identify till the red mark and it does not identify beyond that.. i am trying to identify the elements marked in green.
var iframecount = driver.FindElement(By.XPath("//html/body/form/div[8]/div/div[4]/div[2]/div[2]/div/div/table/tbody/tr/td
Here is the xpath is used taken from FireBug
var iframecount = driver.FindElement(By.XPath("//html/body/form/div[8]/div/div[4]/div[2]/div[2]/div/div/table/tbody/tr/td/div/span/table/tbody/tr/td[2]/span/span/input"));
i have found answer for this...
Sharepoint New item form (i.e modal pop up) has 3 iframes without id or name so switching to iframe using the below code works
driver.SwitchTo().Frame(2);
i.e frames start from 0 index.

Categories

Resources