Not able to click on a link in a frame - selenium

I am not able to click a link in a frame in run mode. First I switched to Frame and then click on a link. After clicking the link, I want to click on another link on the same frame. But not able to click on the second link. In Debug mode, I am able to click both the link.
Used URL: http://demo.guru99.com/selenium/deprecated.html
There are three frames on the page and I switch to frame named "classFrame". On the Frame, I click on the link named "Deprecated". I got all the content of "Deprecated" link. Now I want to click on "Overview" link where I was before. But I am not able to click on link named "Overview". Please help me to click on the second link "Overview".
I am using following code:
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driverChrome = new ChromeDriver();
WebDriverWait webWaitVar = new WebDriverWait(driverChrome, 1000);
driverChrome.get("http://demo.guru99.com/selenium/deprecated.html");
driverChrome.switchTo().frame("classFrame");
driverChrome.findElement(By.linkText("Deprecated")).click();
WebElement linkOverview = driverChrome.findElement(By.linkText("Overview"));
webWaitVar.until(ExpectedConditions.visibilityOf(linkOverview));
driverChrome.findElement(By.linkText("Overview")).click();
For page HTML code, Please refer the link as I am not able to paste it here. Thank you very much

To click() on the element with text as Deprecated within the url http://demo.guru99.com/selenium/deprecated.html and again click() on the element with text as Overview, as the the desired element is within a <frame> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
cssSelector:
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driverChrome = new ChromeDriver();
driverChrome.get("http://demo.guru99.com/selenium/deprecated.html");
new WebDriverWait(driverChrome, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame[name='classFrame']")));
new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.topNav a[href^='deprecated-list']"))).click();
new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.topNav a[href^='overview-summary']"))).click();
xpath:
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driverChrome = new ChromeDriver();
driverChrome.get("http://demo.guru99.com/selenium/deprecated.html");
new WebDriverWait(driverChrome, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[#name='classFrame']")));
new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='topNav']//a[starts-with(#href, 'deprecated-list')]"))).click();
new WebDriverWait(driverChrome, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='topNav']//a[starts-with(#href, 'overview-summary')]"))).click();
Here you can find a relevant discussion on Ways to deal with #document under iframe

Related

How to find the control present in an iframe using selenium?

I want to find the control present in an iFrame in a webpage using selenium to perform actions like click and setting text in textbox. What is the best way to do that using c# or java language.
By using below code you get controls inside iframe in chrome driver. Install latest selenium chromedriver and place it at C:\chromedriver.exe to below code
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
// Initialize browser
WebDriver driver=new ChromeDriver();
//navigate to url
driver.get("https://demoqa.com/frames");
//Switch to Frame using Index
driver.switchTo().frame(0);
//Identifying the heading in webelement
WebElement frame1Heading= driver.findElement(By.id("sampleHeading"));
//Finding the text of the heading
String frame1Text=frame1Heading.getText();
//Print the heading text
System.out.println(frame1Text);
//closing the driver
driver.close();

Trying to hover on a menu and click on a link in the sub-menu but no luck

I am learning to hover over a menu and click on a link int he sub-menu.
The scenario is go to the URL "https://www.amazon.in", hover over "Hello Sign in" and then click on the link "Start here".
I can hover over "Hello Sign in" using moveToElement() and sub menu is opening, but couldn't click on the link "Start here".
Here is my code.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement startHere =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Start here")));
startHere.click();
The link text includes a dot at the end, you missed it in your code, Try By.linkText("Start here.") or By.partialLinkText("Start here")
Please try the below modified code and it's working as expected. First you can find the new Customer Div and then directly access the start here link as below.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement newCustomer=driver.findElement(By.id("nav-flyout-ya-newCust"));
newCustomer.findElement(By.xpath(".//a")).click();
To access the URL https://www.amazon.in then hover over Hello Sign in and then click on the link Start here. you have to induce WebDriverWait for the element with text as Hello Sign in to be visible, then Mouse Hover over it and then induce WebDriverWait for the element with text as Start here. to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement signUp = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-link-yourAccount")));
new Actions(driver).moveToElement(signUp).build().perform();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Start here."))).click();
Browser Snapshot :

Selenium WebDriver Log Out from Facebook

Does anyone know how to click log out button? I tried using driver.findElement(By.linkText("Log out"));
But it returned an error saying element is not found. Is it because the list is dynamically generated?
You should try using WebDriverWait to wait until elementToBeClickable it works for me as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement accountSettings = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Account Settings")));
accountSettings.click() //this will click on setting link to open menu
WebElement logOut = wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Log Out")));
logOut.click() // this will click on logout link
Hope it helps...:)
I assume, after click on arrow button, Log Out button appers in ur code. So to click on that log Out button, use the below portion as cssSelector:
a[data-gt*='menu_logout']>span>span._54nh
driver.findElement(By.cssSelector("a[data-gt*='menu_logout']>span>span._54nh"));

Popup scroll not working in selenium webdriver

I am running my script in mozilla firefox I want to scroll popup I
applied so many methods but doesn't work for me
I used keys.tab to reach that element but it was unable to enter text in that textfield using senkeys("xyz#gmail.com)
I used scroll method
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('youama-email').scrollIntoView(true);");
some exception occur
3.I used Moveto element method but got exception
WebElement element = driver.findElement(By.id("youama-email"));
Actions
actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.perform();
// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop =
arguments[1];",driver.findElement(By.id("<div-id>")), 100);
Please help me to scroll and enter into the email as well as other
fields that will appear after scroll [![enter image description
here][1]][1]
[1]: http://i.stack.imgur.com/D0hqI.png
Try this code. I think what you didn't do was to wait for the element to be visible which i did. See the below code. It is running correctly.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://wyomingvapor.com/");
driver.findElement(By.xpath(".//*[#id='header']/div/div[2]/div/a[1]")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='y-to-register']/input")));
driver.findElement(By.xpath(".//*[#id='y-to-register']/input")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='youama-firstname']")));
driver.findElement(By.xpath(".//*[#id='youama-firstname']")).sendKeys("xyz#gmail.com");
Thread.sleep(2000L);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
If you still stuck then do reply to me, Jyotsana.
Happy Learning :-)

Can we open a link in new tab (not a new window)in selenium and give commands for carrying out tasks

Can we open a link in new tab (not a new window) and give commands for carrying out tasks
And is it possible to force selenium to open it in new tab.(just like we do Right click - open in new tab)
and then how to handle the tabs
Selenium does not provide a native way to open a new tab. How ever you can use workarounds. Every browser has some short cut for opening new tabs.You can use sendskeys method to do that.
Try the following code:
WebDriver DRIVER=new FirefoxDriver();
DRIVER.get("http://google.com");
WebElement El1=DRIVER.findElement(By.xpath("//body"));
El1.sendKeys(Keys.CONTROL,"t");
DRIVER.get("http://google.com");
Selenium Supports Mouse Hover actions and Right Click functionality, where user can right click on link and choose to open in new Tab.
WebDriver driver = new FirefoxDriver();
driver.get(URL);
Actions act = new Actions(driver);
WebElement linkpath = driver.Findelement(by.xpath(path of the link));
act.contextclick(linkpath).perform(); // right click
act.sendkeys("T").perform(); // click on new tab