I have a CSV file with IPs and passwords for cameras. I made a script to go into the website and configure some settings. but the problem is some of these IPs are not working so the page will return 404 not found.
I want to detect these and ignore them, because every time I face a broken page, the for loop stops.
this a portion of my code:
String PathofPage = "http://" p1 "/doc/page/config.asp";
driver.get(PathofPage);
driver.manage().window().maximize();
if( driver.getTitle()=="HTTP 404 Not Found")
driver.quit();
WebElement Username = driver.findElement(By.id("username"));
Username.click();
It's not exiting the browser when it finds the title.
This is an example of it's not finding the website
CodePudding user response:
I'm not sure the page title you are looking for there is actually existing.
What we have seen there is the unique element with id = http404
, so you can look for that element. As following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 4)
String PathofPage = "http://" p1 "/doc/page/config.asp";
driver.get(PathofPage);
driver.manage().window().maximize();
#In case the error presented quit
try{
wait.until(EC.presence_of_element_located((By.ID, "http404")))
driver.quit();
}
#otherwise do what you want to do there
catch(Exception e) {
WebElement Username = driver.findElement(By.id("username"));
Username.click();
}
CodePudding user response:
You can optimize the code block to invoke driver.quit()
probing if the title contains the expected string inducing WebDriverWait for the titleContains()
and you can use the following locator strategy:
try {
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("HTTP 404 Not Found"));
driver.quit();
}
catch(Exception e) {
WebElement Username = driver.findElement(By.id("username"));
Username.click();
}