Selenium: Cannot enter username in https://www.phptravels.net/admin - java

I tried to practices selenium automation in https://www.phptravels.net/admin but i could not enter username.
private By username = By.xpath("//input[#type='text'][#name='email']");
Actions inputAct = new Actions(mngr.getDriver());
inputAct.click().sendKeys("admin#phptravels.com").perform();
mngr.getDriver().findElement(page.getUsername()).sendKeys("admin#phptravels.com");
I search the developer tools the xpath ahs unique element. Why it cannot enter the username. Thanks.

This works for me:
RemoteWebDriver driver = new FirefoxDriver();
driver.get("https://www.phptravels.net/admin");
WebElement emailInput = driver.findElement(new By.ByXPath("//input[#type='text'][#name='email']"));
emailInput.sendKeys("TEST TEST TEST");
driver.close();

you need to bypass detection methods of selenium.
ChromeOptions options = new ChromeOptions();
IWebDriver driver;
options.AddArguments("--disable-notifications");
options.AddArguments("--no-zygote");
options.AddArguments("--disable-accelerated-2d-canvas");
options.AddArguments("--disable-setuid-sandbox");
options.AddArguments("--no-first-run");
options.AddArguments("--disable-2d-canvas-clip-aa");
options.AddArgument($"--user-agent={UA}");
options.AddArguments("start-maximized");
options.AddArguments("--blink-settings=imagesEnabled=false");
options.AddUserProfilePreference("profile.default_content_setting_values.css", 2);
options.AddArguments("--disable-gpu");
options.AddArgument("ignore-certificate-errors");
options.AddExcludedArguments(new List<string>() { "enable-automation" });
options.AddArguments("--disable-blink-features=AutomationControlled");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string title = (string)js.ExecuteScript("Object.defineProperty(navigator, 'webdriver', {get: () => false})");
driver.Navigate().GoToUrl("https://www.phptravels.net/admin");
new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[2]/form[1]/div[1]/label[1]/input"))).SendKeys("jdhfshshdf");

Related

Selenide: How to open Chrome browser with extensions

I've faced with a problem that I'm not able to open Chrome with any extension. I've already added an extension but don't know how to run it properly with Selenide framework. Could you please help me
#BeforeClass
public static void setUp() {
Configuration.browser = "chrome";
System.setProperty("selenide.browser", "chrome");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
}
Selenide : http://selenide.org/2018/01/12/selenide-4.10/
You can set custom capabilities in Configuration, and Selenide will use them when opening a browser:
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserCapabilities.setCapability(SOME_CAP, "SOME_VALUE_FROM_CONFIGURATION");
Also you can set custom webdriver like in #dangi13 answer:
WebDriverRunner.setWebDriver(myDriverWithExtension);
I do not know how to do it in selenide but you can add extension in selenium like this :
public static WebDriver getChromeDriverWithAdblockCrx() {
System.setProperty("webdriver.chrome.driver", "src//main//resources//chromedriver.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src//main//resources//uBlock Origin.crx"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(capabilities);
}
Hope that helps you:).
#sers, #dangi13 Thanks a lot!
But capabilities were not added from Configuration.browserCapabilities. I wrote the following code:
#BeforeClass
public static void setUp() {
Configuration.browser = "chrome";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
Configuration.browserCapabilities = new DesiredCapabilities();
Configuration.browserCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
}
It is known issue that is mentioned on github: https://github.com/codeborne/selenide/issues/676
As workarond I'm using the following option:
#BeforeClass
public static void setUp() {
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
Configuration.browser = "chrome";
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("src/main/resources/uBlock Origin.crx"));
WebDriver webDriver = new ChromeDriver(options);
setWebDriver(webDriver);
}

Selenium: changing proxy in Firefox

i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?
I have code below:
public class SodirRejestracja {
String baseUrl = "http://google.pl";
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;
#BeforeTest
public void beforeTest() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
System.setProperty("webdriver.gecko.driver", driverPath);
driver=new FirefoxDriver(profile);
}
#Test
public void test(){
driver.get("http://google.com");
}
}
Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?
but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"
A sample (not tested but that compiles) that should do it
String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
.setFtpProxy(proxyName)
.setSslProxy(proxyName);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);
This code works
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxy);
driver = new FirefoxDriver(options);

selenium headless chrome java ignore ssl errors

Selenium headless chrome testing with java in unix returns empty page source as
<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
which was due to accessing the secure http (https) website.
Is there a way to ignore the ssl certificate issue? Please let me know how to ignore it.
Selenium Version 3.7.1..java version 1.8.0.144 chrome driver version 2.33
Chrome Version 62+
I gave a try with options below..but it doesn't seem to work.
1. ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.setHeadless(true);DesiredCapabilities capabilities =
DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--
ignore-certificate-errors,--web-security=false,--ssl-
protocol=any,--ignore-ssl-errors=true"));
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
2. DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability (CapabilityType.ACCEPT_INSECURE_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Am i doing this in a right way? Let me know the trick to make it work
Thanks in advance
Complete Code:
WebDriver driver = null;
try {
String filePath = "Path to driver";
System.setProperty("webdriver.chrome.driver", filePath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.setAcceptInsecureCerts(true);
options.addArguments("test-type");
String[] switches = {"--ignore-certificate-errors"};
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList(switches));
capabilities.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://meta.stackexchange.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("PAGE SOURCE : \n" + driver.getPageSource());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
driver.close();
driver.quit();
}
Now you can add capabilities in option. please try following:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--no-sandbox");
options.addArguments("--headless", "--window-size=1920,1200", "--ignore-certificate-errors");
options.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);
driver = new ChromeDriver(options);

Selenium -Core Java Error

I have the following code:
public static void main(String[] args) {
WebDriver wd = new FirefoxDriver();
wd.get("https://www.wordpress.com/");
}
There are no compile time errors with this but as soon as Mozilla opens, it says "This connection is untrusted". I wish to open the url that I've specified in the code.
Open Firefox driver like this -
ProfilesIni allProfiles = new ProfilesIni();
System.setProperty("webdriver.firefox.profile","your custom firefox profile name");
String browserProfile = stem.getProperty("webdriver.firefox.profile");
FirefoxProfile profile = allProfiles.getProfile(browserProfile);
profile.setAcceptUntrustedCertificates (true);
webdriver = new FirefoxDriver(profile);

getting hidden elements in java using selenium giving error

when i select values hidden its showing error as:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: {"method":"partial link
text","selector":"vehicle-make"}
Here is my code:
package section5.advWays.locatingObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class CusXPathUsingAtt1 {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver wd = new FirefoxDriver();
wd.manage().window().maximize();
Thread.sleep(5000); wd.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));
SelectMakedropdown.selectByVisibleText("BMW");
Select YearSelectDropdown = new Select(wd.findElement(By.id("vehicle-year")));
YearSelectDropdown.selectByVisibleText("2011");
Select VehicleSelectDropdown = new Select(wd.findElement(By.id("vehicle-model")));
VehicleSelectDropdown.selectByVisibleText("228i xDrive Coupe");
}
}
How to select those dropdown using selenium webdriver?
There are two things:
I found that you first need to click on an element without which the Select menu won't open. So in my code, I'm first clicking on the element to enable select menu.
There are also some elements that aren't readily available. Say, for example the Year won't get enabled unless Make is entered.
Please see the code below:
WebDriver driver= new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Make')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
Select SelectMakedropdown = new Select(driver.findElement(By.id("vehicle-make")));
SelectMakedropdown.selectByVisibleText("BMW");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Year')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
Select YearSelectDropdown = new Select(driver.findElement(By.id("vehicle-year")));
YearSelectDropdown.selectByVisibleText("2011");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
driver.findElement(By.xpath("//div[contains(text(),'Select Model')]")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
Select VehicleSelectDropdown = new Select(driver.findElement(By.id("vehicle-model")));
VehicleSelectDropdown.selectByVisibleText("128i Cabriolet Base Model");
driver.quit();
UPDATE for Firefox:
I tried a lot, but I'm still unable to identify why isn't the selects working in Firefox. But I still managed to come up with a work around to do the needful. Here I'm using less use of clicks and more of features your application supports.
WebDriver driver= new FirefoxDriver();
driver.get("http://www.tirerack.com/content/tirerack/desktop/en/homepage.html");
WebDriverWait wait = new WebDriverWait(driver, 30);
JavascriptExecutor executor = (JavascriptExecutor) driver;
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Make')]")));
WebElement we1 = driver.findElement(By.xpath("//div[contains(text(),'Select Make')]"));
executor.executeScript("arguments[0].click();", we1);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-make")));
WebElement SelectMakedropdown = driver.findElement(By.id("vehicle-make"));
SelectMakedropdown.sendKeys("BMW");
SelectMakedropdown.sendKeys(Keys.ENTER);
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Year')]")));
WebElement we2 = driver.findElement(By.xpath("//div[contains(text(),'Select Year')]"));
executor.executeScript("arguments[0].click();", we2);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-year")));
WebElement YearSelectDropdown = driver.findElement(By.id("vehicle-year"));
YearSelectDropdown.sendKeys("2011");
YearSelectDropdown.sendKeys(Keys.ENTER);
Thread.sleep(1000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'Select Model')]")));
WebElement we3 = driver.findElement(By.xpath("//div[contains(text(),'Select Model')]"));
executor.executeScript("arguments[0].click();", we3);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("vehicle-model")));
WebElement VehicleSelectDropdown = driver.findElement(By.id("vehicle-model"));
VehicleSelectDropdown.sendKeys("128i Cabriolet Base Model");
VehicleSelectDropdown.sendKeys(Keys.ENTER);
It seems page require to click on dropdown first , Try this code :
wd.findElement(By.xpath("//*[#id='shopByVehicle-search-change']/div[1]/div[1]")).click();
Select SelectMakedropdown = new Select(wd.findElement(By.id("vehicle-make")));
SelectMakedropdown.selectByVisibleText("BMW");
Above will run for sure.

Categories

Resources