Not able to read the model value using selenium - java

I am new to Selenium web driver. So I am trying to do some sample using different websites. In such pattern, I tried to work with OTTO website and trying to add the item to cart. Here comes the issue, On this website, there is a modal window before adding the item to cart.
My question is how to enter the modal window and access the elements inside it? Below is the code that I have tried it with.Click here to view the image for reference
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AutomationTesting {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","/Users/xxx/eclipse-workspace/seleniumTesting/lib/geckoDriver/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.de");
driver.findElement(By.id("lst-ib")).sendKeys("Pepe Jeans");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Pepe Jeans Online-Shop | OTTO")));
driver.findElement(By.partialLinkText("Pepe Jeans Online-Shop | OTTO")).click();
driver.findElement(By.linkText("Pepe Jeans Chiffonkleid »MARY«, im modischen Leo-Muster")).click();
driver.findElement(By.xpath("//ul//li[#data-variationid='594838149']")).click();
driver.findElement(By.id("addToBasket")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("p_layer")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("or_itemAdded__itemInfo")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#data-qa='goToBasket']")));
driver.findElement(By.xpath("//a[#data-qa='goToBasket']")).click();
}
}

The modal window is simulated, not a actual browser window. So the HTML code of elements inside the modal window are in same page. Use DevTool you can simply to verify that.
All content of the modal window come from a DIV node which id is p_layer, So you can find inside element start from #p_layer

Related

Picking element in iframe using Selenium Java / not able to accept GDPR consent window via automation

I am trying to accept the GDPR Consent window via Selenium (Java), which is placed in an iframe. I understood from the lecture and forums that I have to first switch to that frame (which I did) and then click on the button. However, nothing happens.
Can anyone please help?Thanks
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GDPRiframe {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "/Users/DDD/WebDrivers/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://demo.guru99.com/test/");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
WebElement frameelement = driver.findElement(By.xpath("//iframe[#id='gdpr-consent-notice']"));
driver.switchTo().frame(frameelement);
driver.findElement(By.xpath("//button[#id='save']")).click();
}
}
Verified the css of the object via console, if there is no issue with that - appears to be ok. Also, I tried to click on a link within the frame, also not working. Seems as if I was not switched to the frame.
Thanks

Table not loading in chromedriver while creating selenium test cases using eclipse IDE

I am fetching the data from a table using selenium chromedriver. When I am opening the webpage normally using my chrome browser, the table is loading correctly. But during execution with selenium, the webpage is loaded but table is not loading. I also removed the implicit wait and just navigated to the webpage to see if the table is loaded or not but still after 10 mins table is not loading, but it loads pretty fine and instantly in chrome browser.
Here is my code:-
package automation;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class table_handling {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver",".\\lib\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.nseindia.com/market-data/top-gainers-loosers");
WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(20));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='topgainer-Table']/tbody/tr/td[1]")));
List<WebElement> obj=driver.findElements(By.xpath("//*[#id='topgainer-Table']/tbody/tr/td[1]"));
WebElement temp;
for(int i=0;i<obj.size();i++)
{
temp=obj.get(i);
System.out.println(temp.getText());
}
}
}
For your reference, I am attaching 2 screenshots, one of the normal page which loads pretty fine on chrome browser and 1 of the chromedriver in which table is is loading phase everytime.
Normal chrome Browser
Selenium chromedriver
there is a similar question for the website in case at the following link --> Website Loading Only once then access denied - Selenium Webdriver
Probably the anti-bot protection has something to do with it.
Use the presenceOfElementLocated instead of visibilityOfElementLocated and try it:
WebDriverWait wait=new WebDriverWait(driver,Duration.ofSeconds(20));
wait.until(ExpectedConditions.presenceOfElementLocated((By.xpath("//*[#id='topgainer-Table']"))));

Selenium no such element exists

I am trying to perform a simple checkout flow on my staging website, but I cant seem to find the element. I tried to use selenium IDE which works but when it comes to coding in java I keep getting stuck on secure checkout
this is the element button I want to click
<a class="checkout-anchor click-button display-flex vertical-align-center justify-center" href="javascript:void(0);" onclick="onCheckout()" data-stepid="cartstep04">
<img src="https://release.squareoffnow.com/public/assets/images/checkout/svg/secure.svg" class="secure-pic ls-is-cached lazyloaded" alt="">Secure Checkout</a>
This is the code i have written so far
package googleTestCases;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class simpleCartFlow {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","/Users/manavmehta/Desktop/squareoffSeleniumProjects/chromedriver");
WebDriver driver=new ChromeDriver();
driver.get("http://release.squareoffnow.com/");
driver.manage().window().setSize(new Dimension(1440, 789));
driver.findElement(By.linkText("Products")).click();
driver.findElement(By.cssSelector(".store-buy-pro-button")).click();
driver.findElement(By.cssSelector(".pro-twinpack-button")).click();
driver.findElement(By.cssSelector(".whole-purchase-button")).click();
driver.findElement(By.cssSelector(".productAvailability > .click-button")).click();
driver.findElement(By.cssSelector(".giftpackSubmit")).click();
System.out.println("button not clicked");
driver.findElement(By.cssSelector(".checkout-anchor")).click();
System.out.println("button clicked");
driver.quit();
}
}
instead of using a CSS selector I also tried to use linkText but it still didn't work
I keep getting this error
I suspect your issue is due to not waiting long enough for the element to appear. I ran the following successfully using the exact same selectors you had (although with Python instead of Java):
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_base(self):
self.open("https://release.squareoffnow.com/")
self.click_link("Products")
self.click(".store-buy-pro-button")
self.click(".pro-twinpack-button")
self.click(".whole-purchase-button")
self.click(".productAvailability > .click-button")
self.click(".giftpackSubmit")
self.click(".checkout-anchor")
Full disclosure: This particular framework, SeleniumBase is one that I personally built, and it uses smart-waiting to make sure that elements have fully loaded before taking action. Java probably has something similar so that you can wait for the element to be clickable so that you don't have to sleep for an arbitrary amount of time between steps.

Unable to click on Facebook "setting" link through selenium webdriver with java

My Java
package com.palash.healthcare;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class Login {
#Test
#Parameters({"URL","USERNAME","PASSWORD"})
public static void logindata(String url,String Username,String Password)
{
WebDriver driver = new FirefoxDriver();
driver.get(url);
driver.manage().window().maximize();
driver.findElement(By.id("email")).sendKeys(Username);
driver.findElement(By.id("pass")).sendKeys(Password);
driver.findElement(By.id("u_0_v")).click();
driver.findElement(By.id("userNavigationLabel")).click();
//driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//span[#class='_54nh'][text()='Settings']")).click();
List<WebElement> All_List = driver.findElements(By.xpath("//ul[#class='_54nf']"));
for(WebElement li:All_List)
{
System.out.println(li.getText());
if(li.getText().equalsIgnoreCase("Settings"));
li.click();
}
}
}
I am writing the facebook Setting link script in selenium webdriver with java but I am unable to click on the Setting link also i have tried the above code.Can Anybody Help? and for the html about the script you can see the facebook Setting link right above the "logout" button.
Not too sure what's going on with that list. I don't think you'll need that but correct me if I'm wrong.
The web app I do work for is really wonky and sometimes you have to do some weird stuff. Try something like:
Actions actions = new Actions(driver);
WebElement settings = driver.findElement(By.xpath("//span[#class='_54nh'][text()='Settings']"));
actions.moveToElement(settings).build().perform();
settings.click();
This kinda breaks the .click() down into smaller steps.
Took me a long while to get .click() commands down. They behave differently on different web applications.
Let me know if that works.
Have you considered just navigating to the settings page after you log in? It's https://www.facebook.com/settings.
driver.findElement(By.id("userNavigationLabel")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[#class='_54nh'][text()='Settings']")).click();
Got the Answer Just Need to reconstruct the Xpath.

Unable to open File Upload window using Selenium Webdriver

I am new to Selenium Webdriver, i want to open the file upload window after clicking on Browse button but i am not able to open it using webdriver.
Here is my code :
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Login_Page {
static WebDriver driver;
public static void main(String args[])
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 40);
WebDriverWait wait = new WebDriverWait(driver, 40);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.toolsqa.com/automation-practice-form");
driver.findElement(By.id("photo")).click();
}
}
I am not able to see any file upload window.
I am using Firefox 14.0.1 and selenium-server-standalone-2.24.1.jar
Please let me know how can i do it?
Thanks
I guess you want to upload the file after the click on upload button. Even though you can click on the upload button, which will bring you the pop up window, you can't select the files using selenium calls.
So in order to upload the file, you need to do this:
WebElement uploadButton = driver.findElement(//your strategy) //to the upload button
uploadButton.sendKeys("your full path to the file")
And also you need to use latest Selenium version for your corresponding FireFox browser.
Problem is with your firefox version. In mine this script is working smoothly. Once I had such problem,this script will only make Browse button in focus.What you can do is after getting in focus,Send Enter Key.
Add this piece of code after click event.
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER);
use below line:
driver.findElement(By.xpath(.//*[#id='photo']).click();

Categories

Resources