Is there a way to display webdriver instance on iframe site - java

I'm stuck on how can I open the instance of webdriver on my html page using java.
WebDriver driver = new ChromeDriver(); // Open the Application
the code below open the instance of driver chrome.

If you want to start chrome browser, first download chromeDriver from here After that we need to specify this downloaded chromedriver and then start the driver instance
System.setProperty("webdriver.chrome.driver", "\\downloaded driver path");
WebDriver driver = new ChromeDriver();
driver.get("http://URL here/");
Then as said need to switch to frame, so
driver.switchTo().frame("iframe id or name");
//or
driver.switchTo().frame("index like 0, 1");
//or
driver.switchTo().frame(driver.findElement(By.xpath("//iframe"))); //iframe location
//after work in frame is completed, then we need to switch back to default content
driver.switchTo().defaultContent();
Thank You,
Murali

Related

java driver in selenium project

I am using simple project, where you fill in the form. The last part is to click on a element with no id and no text.
I found that i could use java to do it, but how do i initiate the driver in selenium?
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();
I tried
WebDriver driver = New ChromeDriver();
but that only raised a lot new problems. I am using IntelliJ.
You need chromedriver.exe to initiate selenium web driver.
First download the ChromeDriver. You need to download ChromeDriver for your respective Operating system from this link
//Then set the download location.
System.setProperty("webdriver.chrome.driver", "C://Selenium-java//chromedriver_win32//chromedriver.exe");
//Creating an object of ChromeDriver
WebDriver driver = new ChromeDriver();
//launching the specified URL
driver.get("https://www.google.com/");
Then find your element to be clicked using xpath.
If you say you don't have any id or text for click then use any parent element as reference which you can identify then use xpath axes to identify it.
Then you can click on that element directly.
driver.findElement(By.xpath("//form[#id = 'id']/child::button")).click;

How to pass a headless option for my driver using Java and Selenium?

I am setting up a chrome driver with the help of Selenium and Java. I want this driver to be executed headless but I can't find out the way. Can you explain to me what do I need to do?
My code sample:
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICITY_TIME, TimeUnit.SECONDS);
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH); // OS and Browser options
ChromeOptions options = new ChromeOptions(); // create options instance
options.addArguments("--headless"); // add an option
driver = new ChromeDriver(options); // create a driver with the specific options instance
You just need to create a ChromeOptions object in which you need to save the options for your own driver.
To add your own options just use this: options.addArguments(); and in the parenthesis insert your option in string mode.
For more details and documentation please also check here:
http://chromedriver.chromium.org/capabilities
I think this is going to work.

Is this correct - FirefoxDriver driver = new FirefoxDriver();?

I have seen almost everybody using the statement WebDriver driver=new FirefoxDriver(); here we have created an instance of FirefoxDriver class having type as Webdriver;
What if i directly create an instance of FirefoxDriver as FirefoxDriver driver = new FirefoxDriver();
I have gone through many discussion where it is said that Webdriver is an interface. I know what interface is and how it works. I want to know if FirefoxDriver driver = new FirefoxDriver(); is correct when i know that Firefox is the only browser i am gonna use in my selenium script?
As you mentioned in your question that know that Firefox is the only browser you are gonna use in your selenium script then there is no constraint in using the following line of code :
FirefoxDriver driver = new FirefoxDriver();
Here you can find a detailed discussion on What is the difference between ChromeDriver and WebDriver in selenium?

Switching between 2 windows in selenium webdriver with java

I am automating an application using selenium webdriver with java. In that i have to open a browser instance & perform some actions. After that i have to open another browser instance, perform some actions in it & close that instance. Then i have to return the control back to the first browser instance again to perform some other actions.
I tried using :
String winHandleBefore = driver.getWindowHandle();
//then open new instance and perfom the actions
driver.switchTo().window(winHandleBefore);
But this returned an error :
org.openqa.selenium.remote.SessionNotFoundException: no such session
How can i do this? Can anybody help?
When you did driver = new ChromeDriver(); you reinitialized the driver object, which caused the lost of the first window. You can see it by checking the number of window handles after opening the new window
WebDriver driver = new ChromeDriver();
int len = getWindowHandles().size(); // 1 as expected
driver = new ChromeDriver();
len = getWindowHandles().size(); // still 1, has only the new window
To solve this use temporary driver to open the new window
WebDriver tempDriver = new ChromeDriver();
// do some stuff
tempDriver.close();

Changing URL using WebDriver

I'm using Selenium WebDriver (Java) and am trying to change the URL after WebDriver has logged into a page.
Is there a way to either:
Change the URL of the current window, or
Open a new tab and go to another page.
Thanks!
You didn't share any code so i don't know how is your approach about it, I only share my knowledge about this subject.
1) For your first question i think you know how to open a new page with selenium web driver maybe you can use some wait method and then invoke the driver again.
//open browser
driver = new FirefoxDriver();
//login
driver.get("https://www.google.com/");
//set implicit wait
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Then invoke method again for your second request(I am not try this code maybe you need to create new driver object)
driver.get("https://www.stackoverflow.com");
2) For your second question this link help you.

Categories