Accepting cookies with Selenium in Java - java

I am trying to scrape a news webside, but it is not possible to accept the "accept cookies" popup by using the click() method. I can see the button in my HTML code in my browser, but when I use getPageSource() method the code for the button is not included.
Here my code block
public class Webscraping {
public static void main(String[] args) throws Exception{
System.setProperty("webdriver.chrome.driver","C:\\Users\\Marvin\\Desktop\\Webscraping\\chromedriver.exe");
//Pop Up blocken
//ChromeOptions options = new ChromeOptions();
//options.addArguments("disable-popup-blocking");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String url = "https://www.focus.de/";
driver.get(url);
Thread.sleep(5000);
//HTML Code print
System.out.println(driver.getPageSource());
}
}

To click() on the element Akzeptieren within the url https://www.focus.de/ as the the desired element is within a <iframe> 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:
Using cssSelector:
driver.get("https://www.focus.de/");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Iframe title']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[title='Akzeptieren']"))).click();
Using xpath:
driver.get("https://www.focus.de/");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#title='Iframe title']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#title='Akzeptieren']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?

Related

selenium java NGWebdriver

I have tried everything to login into one site using Selenium webdriver java, but there is one "window", which I don't know how to call it , that I couldn't find one way to click in order to access it. Here, I open the firefox browser and lend on the parfumo.net webpage. The site loads "one window" with cookies settings...
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
There is a Iframe you have to first switch to it like below
driver.switchTo().frame("iframeId");
OR
driver.switchTo().frame("iframeName");
Once you switch to the frame than perform the click like below.
public static void invokeBroser() throws InterruptedException {
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
NgWebDriver ngWebDriver = new NgWebDriver(js);
ngWebDriver.waitForAngularRequestsToFinish();
driver.get("https://www.parfumo.net");
driver.manage().window().maximize();
Thread.sleep(3000);
// Initialize and wait till element(link) became clickable - timeout in 60 seconds
WebDriverWait w = (WebDriverWait) new WebDriverWait(driver, Duration.ofSeconds(600));
driver.switchTo().frame("sp_message_iframe_737779");
w.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Accept*')]")));
}
You can switch back to the main document like below:
driver.switchTo().defaultContent();

Selenium: Page gets refreshed after login

After the code clicks the action to login, the page gets refreshed and doesn't redirect to the next page. This only occurs for this website as I am redirected properly on other sites.
When I am giving the wrong credentials even the error message is not getting displayed.(Manually it works)
I am testing in Chrome with Selenium java.
Here is my code:
public class Test {
private static final String HTMLPageSourceCode = null;
public static void main(String[] args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Selenium project\\chromedriver_win32/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://-----/tandem/login");
Thread.sleep(5000);
driver.manage().deleteAllCookies();
driver.findElement(By.id("login")).sendKeys("----");
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.findElement(By.id("password")).sendKeys("----");
WebElement loginbutton = driver.findElement(By.xpath(".//*[#id='login-button']"));
Actions actions = new Actions(driver);
actions.click(loginbutton).perform();
}
}
To login in into the website https://outhouse.inhouseusa.com/tandem/login/ you need to induce WebDriverWait for the elementToBeClickable() and you can use the following Locator Strategies:
Code Block:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
//options.addArguments("disable-infobars");
WebDriver driver = new ChromeDriver(options);
driver.get("https://outhouse.inhouseusa.com/tandem/login/");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.form-control#login"))).sendKeys("sandy");
driver.findElement(By.cssSelector("input.form-control#password")).sendKeys("sandy");
driver.findElement(By.cssSelector("input.pull-right.button.button-primary#login-button")).click();

How to access form type login/registration pop up in Selenium

I am trying to access the following site and get a registration pop up. In HTML it shows as form type. I tried handling as alert but it is not and I get exception as no modal dialog box opened. I tried window handles. The size of the window handles is only 1.
Please help me, so that, I can click on 'Signin' link on registration form and then login.
Website: http://way2automation.com/way2auto_jquery/index.php
System.setProperty("webdriver.gecko.driver", "C:\\mamtha\\Selenium Practice\\GeckoDriver\\geckodriver.exe");
String URL = "http://way2automation.com/way2auto_jquery/tooltip.php";
WebDriver driver = new FirefoxDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(3000);
Set <String> winhandle = driver.getWindowHandles();
System.out.println(winhandle.size());
WebElement sigin = driver.findElement(By.xpath("//a[contains(text(), 'Signin']"));
sigin.click();
driver.findElement(By.xpath("//input[#name = 'username']")).sendKeys("myusername");
driver.findElement(By.xpath("//input[#name = 'password']")).sendKeys("password");
driver.findElement(By.xpath("//input[#class = 'button']")).click();
Modal dialog box is getting opened in the same page itself.so, you don't want to use the window handles. You need to move the focus to the modal dialog box first and then directly access the required element(add some explicit wait condition as well).
Working Code:
System.setProperty("webdriver.gecko.driver", "C:\\mamtha\\Selenium Practice\\GeckoDriver\\geckodriver.exe");
String URL = "http://way2automation.com/way2auto_jquery/tooltip.php";
WebDriver driver = new FirefoxDriver();
driver.get(URL);
driver.manage().window().maximize();
//Explicit wait is added after the Page load
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleContains("Welcome"));
WebElement modalDialogBox=driver.findElement(By.className("fancybox-skin"));
modalDialogBox.findElement(By.xpath(".//a[text()='Signin']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("fancybox-skin")));
WebElement loginDialogBox=driver.findElement(By.className("fancybox-skin"));
loginDialogBox.findElement(By.name("username")).sendKeys("myusername");
loginDialogBox.findElement(By.name("password")).sendKeys("987654321");
loginDialogBox.findElement(By.className("button")).click();
Try the following code.
String URL = "http://way2automation.com/way2auto_jquery/tooltip.php";
WebDriver driver = new ChromeDriver();
driver.get(URL);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
Thread.sleep(5000);
WebElement sign=driver.findElement(By.xpath("//p[#class='text_popup']/a[contains(text(),'Signin')]"));
sign.click();
Thread.sleep(5000);
driver.findElement(By.xpath("//div[#id='login']/form/fieldset[1]/input")).sendKeys("myusername");
Thread.sleep(5000);
driver.findElement(By.xpath("//div[#id='login']/form/fieldset[2]/input")).sendKeys("password");
driver.findElement(By.xpath("//div[#id='login']/form/div/div[2]/input")).click();

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element while trying to login through Selenium with Java

I have tried my best to write a login script in Selenium for the following site https://www.topmba.com/app. Here is my code:
public class TopMba {
String driverPath = "/usr/bin/chromedriver";
WebDriver driver;
String username = "test#gmail.com"; // Change to your username and passwrod
String password = "12345";
// This method is to navigate topmba URL
#BeforeClass
public void init() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("https://www.topmba.com");
}
// To log in topmba
#Test
public void login() {
driver.findElement(By.className("tm-user")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//*[#id=\"tm-modal-frame-nvtfa7vvbm\"]")));
driver.findElement(By.id("edit-user")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().defaultContent();}
#AfterClass
public void quit() {
driver.close();
}
Here is the Exception :
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="tm-modal-frame-nvtfa7vvbm"]"}
Use the below code:
driver.get("https://www.topmba.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String parentWindowHandle = driver.getWindowHandle();
driver.findElement(By.className("tm-user")).click();
WebElement iframe = driver.findElement(By.xpath("//iframe[contains(#src,'app/sso/user/login')]"));
driver.switchTo().frame(iframe);
driver.findElement(By.id("edit-name")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().window(parentWindowHandle);
There are a couple of things you need to take care as follows :
To click on the icon with tooltip as Login you have used :
driver.findElement(By.className("tm-user")).click();
If you look at the HTML this Locator Strategy identifies the element uniquely but for a more focused click you need to target the <span> tag which is within an <a> tag which is within the <li> tag with the class attribute you have used. Of-coarse you have induce WebDriverWait.
Once the Sign In Dialog Box opens up you will observe the login fields are within an <iframe>. So you have to induce WebDriverWait for both the cases, once for the frame to be available and for the desired element to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.topmba.com");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.tm-user>a.tmba-user>span.fa-img-icons.fa-img-user"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='/app/sso/user/login']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-name"))).sendKeys("khawar");
driver.findElement(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-pass")).sendKeys("khawar");
driver.findElement(By.cssSelector("button.btn.btn-warning.btn-block.button.js-form-submit.form-submit#edit-submit")).click();
Browser Snapshot :

Xpath not working with selenium Web Driver

I have a problem triying to recognize a xpath from the following web page
http://smartchanneltech.com/top100canalti/
This is the element I want to recognize: https://imgur.com/a/ENOP1
This is the xpath that I´m using:/html/body/div/div/div[1]/h1/a
This is the code I´m using:
public WebElement Empresa (WebDriver driver, int Iterator) {
//WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/div[1]/h1/a")));
return driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a"));
}
And, finally, this is the error log: https://imgur.com/a/quFjg
I tried just to driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a")); but is not working also.
Can you help me with this please?
It is inside a iframe. First switch to the frame and try identifying it.
public WebElement Empresa (WebDriver driver, int Iterator) {
driver.switchTo().frame(0);
String xpath="/html/body/div/div/div[1]/h1/a";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return driver.findElement(By.xpath(xpath));
}
If you look into the HTML DOM the WebElement is within an <iframe>. So we need to switch to the <iframe> first with proper WebDriverWait and then locate the WebElement as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
return driver.findElement(By.xpath("div[#class='lb-leaderboard-header']/h1/a[#class='lb-leaderboard-name']"));
I have update the method as below.
Please use this.
public WebElement Empresa (WebDriver driver, int Iterator) {
//WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
WebElement elem = driver.findElement(By.className("lb-leaderboard-widget-wrapper"));
WebElement elemH1 = elem.findElement(By.tagName("h1"));
WebElement elemIWant = elem.findElement(By.tagName("a"));
System.out.println(elemIWant.getAttribute("innerHTML").toString());
return elemIWant;
}
let me know your feedback.

Categories