i am using selenium to open a site but inside site there is a hyperlink that opens a new tab. how can i take screenshot of that newly opened tab by navigating to it.
eg
import selenium.webdriver
import selenium.common
options = selenium.webdriver.firefox.options.Options()
# options.headless = True
with selenium.webdriver.Firefox(options=options) as driver:
driver.get('url')
time.sleep(2)
root=driver.find_element_by_tag_name('html')
root.screenshot('full page screenshot
But it does not work with new opened tab
You need to switch to that window/tab to interact with it.
how-to-switch-to-new-window
driver.switch_to.window(driver.window_handles[1]) #1 being the second window
Related
As per Difference between webdriver.Dispose(), .Close() and .Quit()
driver.close closes the current active window where driver.quit closes all of the windows. What I get is if I opened a new tab in my current window and then called driver.close the whole windows including my tabs would close. But when I run that code
driver.get("http://testingpool.com/selenium-ide-part-1/");
driver.findElement(By.xpath("//*[#id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()
only the first tabs gets closed. Also I find that my allWindowHandles has length of 2 although I have only one window.
I was trying to open a new window using Selenium as per https://www.tutorialspoint.com/how-to-open-a-new-window-on-a-browser-using-selenium-webdriver-for-python using
((JavascriptExecutor)driver).executeScript("window.open('')");
but that resulted in a new tab not a new window.
I am confused if Selenium differentiates between a tab and a window at all or not.
With the terminology used to distinguish between driver.close() and driver.quit() methods supported by Selenium WebDriver in the post you are referencing to, they actually mostly mean browser tabs, not windows.
By opening a new browser window with
((JavascriptExecutor)driver).executeScript("window.open('')");
or by clicking on some web element opening a new browser tab/window is will normally open a new tab in the existing browser.
What I get is if I opened a new tab in my current window and then
called driver.close the whole window including my tabs would close.
But when I run that code
See you may have opened a new tab by clicking on link or using JavascriptExecutor but Selenium still has focus on first tab of the first window.
Therefore, when you do this:
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()
It should close the first tab or window, since you have not switched to new tab/window.
Also I find that my allWindowHandles has length of 2
this is cause you have opened a new tab using this line
driver.findElement(By.xpath("//*[#id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
the moment you open a new tab by clicking on a link or Using JavascriptExecutor, you will have +1 windows handle.
However, if you do not switch to any tab or window/s. Selenium will always have focus on first tab of the first window.
Also, window.open('') is Js command not Selenium.
there is a difference between a new tab or windows. But if you take Selenium into consideration, We switch to new tab or windows in a same way.
However to open a new window, you would have to simulate CTRL+N keyboard action.
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
driver = webdriver.Chrome(r'C:\Users\arush\OneDrive\Desktop\chromedriver.exe')
driver.get('https://www.codingninjas.com/');
# options = Options()
# options.add_argument("--disable-notifications")
# browser = webdriver.Chrome(r'C:\Users\arush\OneDrive\Desktop\chromedriver.exe', chrome_options=options)
# driver.implicitly_wait(1000)
time.sleep(10)
driver.find_element_by_xpath('/html/body/jdiv/jdiv/jdiv[3]/jdiv[1]/jdiv/jdiv').click()
# driver.implicitly_wait(100)
# time.sleep(10)
driver.save_screenshot(r"C:\Users\arush\OneDrive\Desktop\screenshots\cn.png");
driver.quit()
image = Image.open(r"C:\Users\arush\OneDrive\Desktop\screenshots\cn.png")
# Showing the image
image.show()
But problem is some websites have a pop-up box, how do i get a screenshot after closing the popup box.
you have to tell selenium to close the popup. first you need to find the html element for the close button. then click the button using selenium.
i assume it is a html popup and not a javascript alert() popup.
to find the html element: open the website as human. see the popup box. see the close button. right click on the element and select "inspect element" or something similar. the developer tools of your browser should open showing the html source with the close button element highlighted.
now write the code for selenium to find the close button element. usually looks something like this:
closebutton = driver.find_element_by_id('some id')
(if you are lucky and the element has an id)
or
closebutton = driver.find_element_by_xpath('some horribly long xpath expression')
then click the button
closebutton.click()
then do the screenshot
if the popup is a javascript popup you can close it like this
alertbox = driver.switch_to.alert
alertbox.accept()
I have selenium.support version 3.141.0, chrome driver version 2.43.0 and Google Chrome version 71.0.3578.98 and Selenium.ChromeDriver.dll 2.43.0.
when i click on a certain button a new window should be open. i click on the button and if i use any action on the browser for example new WebDriverWait(_driver, TimeSpan.FromSeconds(60)).Until(IsPageLoaded); the window stop to load and stay blank data. why can't i get the browser to load it's content?
Edit: when the new window opens i change the the driver to the latest windowHandel and use the webDriverWait from above to wait which result in a blank data window, but if i use thread.sleep after changing the windowHandel the window load its content
You can try with a explicit wait for a element to be visible on the new window. That would ensure it waits for the contents to load up
I am using selenium web driver using phpunit, the problem I am facing right now is that I have a link button and when I click this button the current link is opened in a new page and I have no idea about how to oen the currently opened new page, can some one help me about how can I code in PHPUnit to open the currently new opened page
Use "WindowHandles" in PHPunit function
Here is the solution to navigate between the tabs:
If there are two opened tabs in your browser and you want to switch to the next tab, below code will work:
$window_handles = $this->windowHandles();
$this->window($window_handles[1]);
And if you want to switch back to the previous tab just change the index as shown below :
$window_handles = $this->windowHandles();
$this->window($window_handles[0]);
I try to click on an element, which opens an windows model (choose a file..).
When I try to click on this button, no upload button is opened (But when I click on it manually it opens. Also, I see WebDriver success to find the element - the button is marked, but no window is opened.)
I try the next, none of them works:
1. currentPopup.click();
2. new Actions(driver).click(currentPopup).perform();
3. JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", currentPopup);
Do not click that button. Selenium does not popup the windows open file.
The work around is
driver.findElement(By.id("UploadElementID")).sendKeys("<absolutePathToFile>");
UploadElementID is the id of that element (input type="file") and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you