Hi all IM having troubles. My test click an hyperlynk and a new window is displayed. the problem is that when I implement windows handlers selenium only identify one window instead of 2 window and when I try to find the element of the window, well I cant cause for selenium the window never exist :(
driver.switchTo().frame(CQLo.getMframe());
WebElement Rad_CIT = (new WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(CQLo.getRadCIT()));
Rad_CIT.click();
WebElement Text_CIT = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(CQLo.getTextCIT()));
Text_CIT.clear();
Text_CIT.sendKeys(citbox);
setLog("CITBox: " +citbox);
//WebElement Link_WFM =
Actions act = new Actions(driver);
WebElement onElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(CQLo.getLink_WFM_Admin()));
act.contextClick(onElement).perform();
act.sendKeys("o").perform();
Set <String> wind_ows = driver.getWindowHandles();
for(String sw : wind_ows)
{ System.out.println(""+sw.toString());}
setLog("Open log on " +citbox);
Thread.sleep(2000);
WebElement Text_UserN = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(Elog.getUserName()));
Text_UserN.sendKeys(MSR_name);
WebElement Text_UserP = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(Elog.getUserPassword()));
Text_UserP.sendKeys(MSR_pass);
WebElement B_logon = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(Elog.getB_logon()));
B_logon.click();
after printing the SET with the windows I only receive one window
Wait some time before you get window handles after performing click on element. I think by the time you read driver window handles, new window might not be opened.
Related
I have added robot class to drag from left to right in the frame but it is not working I also use action builder drag and drop it is not working somehow I use robot class so it clicks on the header but does not do drag and drop
Here is my code :
WebDriverWait waitDrag = new WebDriverWait(driver, Duration.ofSeconds(30));
waitDrag.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id=\"root\"]/div/div/div[2]/div[2]/div/div[1]/div[2]/div/div[1]/div/div/div/div[1]/div")));
WebElement fromWebElement =driver.findElement(By.xpath("//*[#id=\"root\"]/div/div/div[2]/div[2]/div/div[1]/div[2]/div/div[1]/div/div/div/div[1]/div"));
WebElement toWebElement = driver.findElement(By.xpath("//div[#class=\"react-flow__pane\"]"));
Point coordinates1 = fromWebElement.getLocation();
Point coordinates2 = toWebElement.getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates1.getX(), coordinates1.getY());
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(coordinates2.getX(), coordinates2.getY());
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);
I am trying to web scrape a Quebec government website for law names and their associated PDFs but when I try to open the tabs of all the different laws to get their PDF links, I get an ElementNotInteractable Exception when it attempts to open the 9th link. I tried opening the link by itself and it opens fine but when it is going through all the laws, it stops there and gives me that exception. Here is my code snippet:
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
Thread.sleep(5000);
List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.xpath("//body/div/div/div[2]/div/div[2]/table/tbody/tr[contains(#class, 'clickable')]/td/a"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedStatutes.size(); i++){
String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
}
There are several issues here:
The main problem is that you have to scroll the element you want to click on into the view. Your default initial screen height presents 8 rows while to click on 9-th row and more you have to scroll that element first into the view.
You could set driver window to better dimensions, this will show you more screen, however you will still have to scroll, but after 15 elements.
You should improve your locators.
You should not mix up WebDriverWait and implicitlyWait.
This should work better:
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
driver.manage().window().maximize();
driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("tr.clickable a"), 100));
Thread.sleep(300);
List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.cssSelector("tr.clickable a"));
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedStatutes.size(); i++){
String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", QuebecConsolidatedStatutes.get(i));
Thread.sleep(300);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
}
}
Iam new to selenium and trying to switch to new amazon tab which I opened and print the title
But when Sending control and Tab nothing happens how to achieve it can anyone help here :)
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.com");
//Thread.sleep(6000);
Actions a = new Actions(driver);
List<WebElement> el= driver.findElements(By.xpath("(//div[#class='navFooterColHead'])[1]/../ul/li/a"));
for(int i=0;i<el.size();++i)
{
el.get(i).sendKeys(Keys.Control,Keys.Enter);
}
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
I'm trying to get Whatsapp QR with Selenium WebDriver on remote machine.
I using latest version on docker package
selenium/standalone-chrome:latest
The point is, if I try the following code without headless on, works smooth.
Maybe my configuration isnt correct.
Any ideas?
ChromeOptions dCap = new ChromeOptions();
dCap.setHeadless(true);
dCap.setCapability("platform", "LINUX");
dCap.setCapability("version", "latest");
dCap.addArguments("disable-infobars");
dCap.addArguments("--start-maximized");
dCap.addArguments("--disable-extensions");
dCap.addArguments("--disable-gpu");
dCap.addArguments("--window-size=1920x1080");
dCap.addArguments("--enable-javascript");
String driverPath = System.getProperty("user.dir") + "/exe/chromedriver";
System.setProperty("webdriver.chrome.driver", driverPath);
URL rutaProxy = new URL("http://localhost:4444/wd/hub");
WebDriver driver = new RemoteWebDriver(rutaProxy, dCap);
driver.get("https://web.whatsapp.com/");
WebDriverWait espera = new WebDriverWait(driver, 20);
espera.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".landing-main canvas")));
WebElement canvas = driver.findElement(By.cssSelector(".landing-main canvas"));
JavascriptExecutor js = (JavascriptExecutor)driver;
String imagenBase64 = (String) js.executeScript("return arguments[0].toDataURL('image/png').substring(21);", canvas);
//TEST
byte[] imageByte = Base64.getDecoder().decode(imagenBase64.substring(1));
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
BufferedImage image = ImageIO.read(bis);
bis.close();
File outputfile = new File("image.png");
ImageIO.write(image, "png", outputfile);
driver.close();
I've been in this situation, add more time delay between a action and a window popup, because the refresh of some DOM elements takes time. Enabling headless requires more latency than not enabling it.
How to switch control to new window if new window is opening only after closing the existing window?
I am using Selenium and Internet Explorer.
for (String h : webDriver.getWindowHandles()) {
webDriver.switchTo().window(h);}
WebElement element= webDriver.findElement(By.xpath("//*[#id='Standard']/p[1]"));
Actions a = new Actions(webDriver);
a.moveToElement(element).build().perform();