In Moneycontrol's website, I am unable to enter the username and password while trying to log in. Selenium is unable to find the Webelements.
public void setUrl() throws IOException {
driver = new FirefoxDriver()
driver.get("http://www.moneycontrol.com/");
}
public void Login() {
driver.findElement(By.xpath("//a[#title='Log In']")).click();
//enter user name and password
driver.findElement(By.xpath("//div[#class='formbox']/div[1]/form/div[1]")).sendKeys("xyz#gmail.com");
driver.findElement(By.xpath("//input[#id='pwd']")).sendKeys("Abc#92");
screenshot
The Email or User ID field is within an <iframe>, so to invoke sendKeys() at Email or User ID and Password field you have to switch to the respective <iframe> as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='https://accounts.moneycontrol.com/mclogin/?d=2']")));
driver.findElement(By.xpath("//form[#id='login_form']//input[#class='textfield' and #id='email']")).sendKeys("xyz#gmail.com");
driver.findElement(By.xpath("//form[#id='login_form']//input[#class='textfield' and #id='pwd']")).sendKeys("Abc#92");
**Selenium with C#**
You need to switch to frames and in your case id of your frame is "myframe". Below is the working code for selenium with C#
[TestMethod]
public void moneyControl()
{
IWebDriver driver = new ChromeDriver(#"C:\Users\Akash\Downloads\chromedriver_win32\");
driver.Navigate().GoToUrl("http://www.moneycontrol.com/");
driver.Manage().Window.Maximize();
driver.FindElement(By.XPath("//a[#title='Log In']")).Click();
driver.SwitchTo().Frame("myframe");
driver.FindElement(By.Id("email")).SendKeys("xyz#gmail.com");
driver.FindElement(By.Id("pwd")).SendKeys("abc");
}
Related
I am trying to find the element of the field First Name on the page https://whitelabel.sandbox.array.io/signup?platform=v3. I tried searching by id, classname, name, cssSelector, etc. but none works. I even added waiter to ensure it is loaded well before I try to find the element. Same issue happens for all fields in the page. So, the issue is not unique to this field.
Tried this in Chrome and Firefox on Mac. The same code works well to find the username field in gmail.com page.
driver.get("https://whitelabel.sandbox.array.io/signup?platform=v3");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10));
WebElement selectFirstName = driver.findElement(By.name("firstName"));
//assertNotNull(driver.findElement(By.name("firstName")));
//assertNotNull(driver.findElement(By.xpath("//input[#name='firstName']")));
//driver.findElement(By.cssSelector("input[name='firstName']")).sendKeys("Thomas");
//driver.findElement(By.name("firstName")).sendKeys("Thomas");
//driver.findElement(By.xpath("//input[#name='firstName']")).sendKeys("Thomas");
//page.locator("[name='firstName']").type("Thomas");
//driver.findElement(By.cssSelector("input[name='firstName']")).sendKeys("Thomas");
//driver.locator("[name='firstName']").type("Thomas");
Error that I get is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"css
selector","selector":"*[name='firstName']"} (Session info:
chrome=103.0.5060.134)
Does anyone know what I need to do differently to be able to get the webElement?
The First Name field within the website https://whitelabel.sandbox.array.io/signup?platform=v3 is within a #shadow-root (open)
ShadowRoot in selenium4
As per the test implementation in ShadowRoot.java:
import static org.openqa.selenium.remote.Dialect.W3C;
import static org.openqa.selenium.remote.DriverCommand.FIND_ELEMENTS_FROM_SHADOW_ROOT;
import static org.openqa.selenium.remote.DriverCommand.FIND_ELEMENT_FROM_SHADOW_ROOT;
// Note: we want people to code against the SearchContext API, so we keep this class package private
class ShadowRoot implements SearchContext, WrapsDriver {
private final RemoteWebDriver parent;
private final String id;
ShadowRoot(RemoteWebDriver parent, String id) {
this.parent = Require.nonNull("Owning remote webdriver", parent);
this.id = Require.nonNull("Shadow root ID", id);
}
#Override
public List<WebElement> findElements(By by) {
return parent.findElements(
this,
(using, value) -> FIND_ELEMENTS_FROM_SHADOW_ROOT(id, using, String.valueOf(value)),
by);
}
#Override
public WebElement findElement(By by) {
return parent.findElement(
this,
(using, value) -> FIND_ELEMENT_FROM_SHADOW_ROOT(id, using, String.valueOf(value)),
by);
}
#Override
public WebDriver getWrappedDriver() {
return parent;
}
public String getId() {
return this.id;
}
private Map<String, Object> toJson() {
return singletonMap(W3C.getShadowRootElementKey(), id);
}
}
#titusfortner explains the same in their comment as:
The actual state is that the return value of that JavaScript changed in v96 of ChromeDriver in order to be w3c compliant. Selenium 3.141.59 can not parse this new return value. You can use getShadowRoot() in Selenium 4, or you'll be able to get a ShadowRoot instance returned from the JS in Selenium 4.1.
And I stand corrected, you need to cast to SearchContext interface.
Solution
To send a character sequence within the First Name you can use the following Locator Strategy:
Code Block:
driver.get("https://whitelabel.sandbox.array.io/signup?platform=v3");
WebElement element = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.tagName("array-account-enroll")));
SearchContext context = element.getShadowRoot();
WebElement firstName = context.findElement(By.cssSelector("input[name='firstName'][placeholder='Enter first name']"));
firstName.sendKeys("Supramanian");
Browser snapshot:
I am new to selenium web testing i have automated a Sign-In process for a web base application. now making it for Sign Up process i am stuck at a point where a verification code is sent to a mail address and then i have to copy that into my verification code field and proceed further
As i have searched so far i came to know about the mailosaur server but unable to copy that email verification code into my automated web browser. i also searched for the tutorials but unable to find any useful resource. also i want to generate random emails that part is also not getting in my mind.
As i am new to selenium so it is requested to please provide detail answer so i can understand it better, Thanks in advance, working on Intellij, Mavaen (Java)
You can use mailinator.com. No need to register or create a mail box. In your app just enter email with made up name #mailinator.com (asad1#mailinator.com, asadXY#mailinator.com, whatever).
To collect confirmation link (double opt in) I'm using this:
public class Mailinator {
public WebDriver driver;
public Mailinator(WebDriver driver) {this.driver = driver;}
public String urlMailinator = "https://www.mailinator.com/";
public WebDriverWait waitSec(WebDriver driver, int sec) {return new WebDriverWait(driver, sec);}
public static String doubleOptInLink = null;
public String getDoubleOptInLink() {return doubleOptInLink;}
public void setDoubleOptInLink (String doubleOptInLink) {Mailinator.doubleOptInLink = doubleOptInLink;}
public void collectDoubleOptInLink(String userEmail, int expectedNumberOfDeliveredEmails) throws InterruptedException {
driver.get(urlMailinator);
WebElement fldInbox = waitSec(driver, 5).until(ExpectedConditions.elementToBeClickable(By.id("inboxfield")));
fldInbox.sendKeys(userEmail);
WebElement btnGo = driver.findElement(By.xpath("/html/body/section[1]/div/div[3]/div[2]/div[2]/div[1]/span/button"));
btnGo.click();
waitSec(driver, 600).until(ExpectedConditions.numberOfElementsToBe((By.xpath("//*[#id=\"inboxpane\"]/div/div/div/table/tbody/tr")), expectedNumberOfDeliveredEmails));
WebElement lastMailLink = driver.findElement(By.xpath("//*[#id=\"inboxpane\"]/div/div/div/table/tbody/tr"));
lastMailLink.click();
Thread.sleep(3000);
driver.switchTo().frame(driver.findElement(By.id("msg_body")));
setDoubleOptInLink(driver.findElement(By.xpath("//*[#id=\"intro\"]/tbody/tr/td/a")).getAttribute("href"));
}
}
In my scenario:
register to webapp with new made up email, confirmation email is send
using collectDoubleOptInLink(email, 1); is the confirmation link set as doubleOptInLink
calling another method to go to the confirmation link with getDoubleOptInLink();
Sure you will need change what string comes to setDoubleOptInLink();
In specific cases don't forget to setDoubleOptInLink(null);.
I want to run test JUnit i work in 2 different projets :
In the first one i have
The feature :
Feature: Google Search
Scenario: Validate google search text field
Given open a browser
When navigate to google page
Then validate search text field
The Step definition Class :
#Given("^open a browser$")
public void open_a_browser() throws Throwable {
System.setProperty("webdriver.chrome.driver", "D:\\Drive\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
}
#When("^navigate to google page$")
public void navigate_to_google_page() throws Throwable {
driver.get("http://www.google.com");
}
#Then("^validate search text field$")
public void validate_search_text_field() throws Throwable {
driver.findElement(By.name("q")).sendKeys("selenium "); }
And the Runner class :
#RunWith(Cucumber.class)
#CucumberOptions(features={"src\\test\\resources\\com\\tutoriel\\testing\\features\\Googlesearch.feature",
glue={"com\\tutoriel\\testing\\stepdefinitions"})
public class RunTestRunner {
}
And it works for me very good
but when i tr to add those 3steps (feature, stepdef and the runner ) in an big another project it doesn't work
i don't undersand the issue !!
this is the error
You can implement missing steps with the snippets below:
but i have already defined them in the class steps definition
Anyone can help me please
Screenshot of the element I want to click:
I automating my website(new to automation). once i login i get to another page where selenium web driver is not able to find any of the elements(I tried all possibilities even sso related).
Only solution i could find was using tabs and enter.
So when i enter that page i need to click 9 time "TAB" key from the keyboard and then enter so that my login is verified. since i don't have any element using which i can perform the tab and enter actions. is there a way where once i get to that page the web driver starts pressing "TAB" key 9 times and then "Enter" on 10 time.
Please help I have been working on this over a week now and not getting
anywhere.
optimist_creeper-main class:
package Modules;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import Modules.HomePage;
public class MainClass {
String appUrl = "als-stg-1.mtvn.ad.viacom.com/webqa/";
#Test public void MainTest() {
System.setProperty("webdriver.gecko.driver", "C:\\Shayni Coding\\Automation\\Gecko\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(appUrl);
HomePage home = new HomePage();
home.HomePageTest(driver);
}
}
Home Page class:
public class HomePage {
#BeforeClass public void beforeClass() {
System.out.println("before class");
}
public void HomePageTest(WebDriver driver) {
driver.manage().window().maximize();
WebElement email = driver.findElement(By.id("cred_userid_inputtext"));
email.sendKeys("shayni#outlook.com");
WebElement pass = driiver.findElement(By.id("cred_password_inputtext"));
pass.sendKeys(Keys.ENTER);
pass.click();
String expectedTitle = "VMS Web";
String actualTitle = driver.getTitle();
Assert.assertEquals(expectedTitle,actualTitle);
}
}
Thanks.
Just get a random object like the body tag and use that to send your key presses.
e.g.
WebElement dummyElement = driver.findElement(By.xpath("/html/body"));
for (int i = 0; i < 9; ++i) {
dummyElement.sendKeys(keys.TAB);
}
dummyElement.sendKeys(keys.ENTER);
The above code finds the body take and sets it as an element. It then presses the tab key 9 times and then presses the enter key. Which is what you asked for. Hope that helps.
Can you please let me know how we can logout in chrome browser by using selenium?
e.g
public class AJ {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com");
WebElement element=driver.findElement(By.name("email"));
element.sendKeys("user#example.com");
element=driver.findElement(By.name("pass"));
element.sendKeys("password");
element.submit();
The following code should help you.
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://facebook.com");
WebElement element=driver.findElement(By.name("email"));
element.sendKeys("user#example.com");
element=driver.findElement(By.name("pass"));
element.sendKeys("password");
element.submit();
//Click on dropdown menu then logout button
driver.findElement(By.id("userNavigationLabel")).click();
driver.findElement(By.id("logout_form")).click();
//Check to see if email login box is available
//therefore confirming user has logged out
driver.findElement(By.name("email"));
}
I recommend using the Chrome Developer tools to help you find unique attributes of a page for Selenium to find.
I hope this helps!
In python the same is done using this line of code. It uses the same module i.e, Selenium.
So just change the element using css selector by using the argument passed below.
logout1 = driver.find_element_by_css_selector("._w0d[action='https://www.facebook.com/logout.php?button_name=logout&button_location=settings']").submit()
Hope it works.
I am able to successfully logout from Facebook.
Here is the Java code
String url = "http://facebook.com";
String email = "email";
String password = "password";
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.get(url);
driver.manage().window().maximize();
driver.findElement(By.id("email")).sendKeys("Your email here");
driver.findElement(By.id("pass")).sendKeys("Your password here" + Keys.ENTER);
driver.findElement(By.id("logoutMenu")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//form[contains(#id,'show_me_how_logout')]/../../../..")).click();