I'm currently trying to open an URL in a browser with options of different browsers. example if I give chrome then the URL ("https://www.google.com/") should open in chrome, likewise in firefox.
Document doc = Jsoup.connect("http://example.com")
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
The above code does NOT OPEN the browser!
If you want to open a browser you need to use selenium, the following example will give you a brief idea about your need. please try to grasp as much as you can from this ex. feel free to comment for any queries.
package selenium_Methods;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Bozoinabusride_Search {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\Tushar\\JARs\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://bozoinabusride.blogspot.com/");
driver.manage().window().maximize();
WebElement searchThis = driver.findElement(By.name("q"));
searchThis.sendKeys("silver");
driver.findElement(By.className("gsc-search-button")).click();
}
}
Related
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
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']"))));
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.
I am following a code example in selenium and I keep getting the error "Error: Could not find or load main class Test." I am not sure what is causing this as I downloaded and unzipped selenium and extracted the jar files in Eclipse. I also have the class "Test" under the appropriate file and I am not sure what is causing this problem. Here is the code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
Update your run configuration in Eclipse. It should point to Example class.
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();