I am running Selenium to test a specific area of my companies webpage. What I am trying to do seems relatively easy in theory, but I have ran into several obstacles. Can someone please tell me why the URL is opening but won't select the "Services" Hyperlink?
Below is a snippet of the code:
System.setProperty("webdriver.ie.driver","Path to IE/IEDriverServer_64.exe");
WebDriver driver = new InternetExplorerDriver();
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
driver.get("https://www.ups.com/us/en/Home.page");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*/div[#id='ups-header']/nav[#id='ups-navItems']/ul[#class='ups-navItems_primary']/li[#class='ups-navMenu ups-menu'][3]/a[#id='ups-menuLinks2']")).click();
As I stated earlier, in theory this should open up UPS's homepage and select the "Services" tab on the top right of the page. Instead it just goes to UPS.com homepage and stays there.
I have driver.findElement(By.xpath........); in this example but I have tried findElement(By.name & partialLinkText
Can anyone give me a solution besides update to latest version(s)?
Metadata:
Windows 10,
JAVA 10,
Internet Explorer (Unfortunately) 11.4.
Thanks in advance!
Edit with additonal HTML structure:
This is a portion of the HTML I am working with. This HTML belongs to the Services link I want to click in my automation:
<a role="button" href="#" class="ups-analytics ups-menu_toggle" data-
content-block-id="M1" data-event-id="22" aria-expanded="false" id="ups-
menuLinks2" aria-controls="ups-menuPanel2">Services<span class="ups-mobnav-
arrow" aria-hidden="true"></span></a><div class="ups-menu_list ups-cols-3"
aria-hidden="true" role="region" id="ups-menuPanel2" aria-labelledby="ups-
menuLinks2">
<h2 class="ups-med_show">Services</h2>
<div class="ups-menu_listCols">
Internet Explorer v11 doesn't opens the url on my system but with Selenium v3.12.0, ChromeDriver 2.39 and Chrome v67.0 the following solution clicks on the element with text as Services just perfecto:
Code Block:
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.ups.com/us/en/Home.page");
driver.findElement(By.linkText("Services")).click();
Browser Snapshot:
This was tricky for IE , for some reasons all the xpath i tried seems to not work. This might be due to some capabitlites that needs to be added. I haven't looked into it in details but its working for me in Firefox:
String baseURL = "https://www.ups.com/us/en/Home.page";
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "//path to//geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(baseURL);
Thread.sleep(3000);
WebElement service=driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/div/div/div/header/div/nav/ul[1]/li[3]"));
System.out.println(service.getText());
service.click();
If you have issues with your Firefox. Please share the stacktrace so we can help. This code perfectly works for Firefox
Related
I am using the latest chrome driver with selenium to test a web application hosted on Microsoft Azure.
The script starts by logging into the web application. An authentication window opens that requires the user to login through Azure and then press a, "Grant" button that will allow the web application to speak to a Therefore database to populate a few metadata fields.
This all works perfectly when headless mode is disabled. However, when headless mode is enabled it seems as though the, "Grant" button doesn't function. I am logging and taking screenshots during this process, which is how I know that the "Grant" button element is found and is being clicked. The button becomes highlighted when clicked, which is shown in the screenshot, but nothing happens and the authentication window times out in headless which kills the test.
I have tried different clicking methods, but this made no difference as the element is found and is being clicked. I have also pressed the, "Sign in as a different user" button on the form to ensure that the .click() method is functioning as expected, which of course works. I tried to add longer wait times but to no avail.
I have also added the following Chromium driver options:
ChromeOptions options = new ChromeOptions();
options.addArguments("enable-automation");
options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1920,1080");
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-gpu");
options.addArguments("--incognito");
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--allow-insecure-localhost");
options.addArguments("--disable-popup-blocking");
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
How I am clicking the element:
System.out.println("Grant permission...");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Grant')]"))).click();
What baffles me is how this works seamlessly when headless is disabled but not when it's enabled. I'm wondering if this could be a Chrome driver issue? However I know that this is unlikely.
Any recommendations are appreciated, thanks.
Adding button HTML as requested:
<form method="POST">
<p>Hello, Test</p>
<ul>
<li class="text-left">Act with your access permissions</li>
<li class="text-left">Allow continuous access while you are not online.</li>
</ul>
<p>
<button type="submit" name="submit.Grant" value="Grant" class="btn btn-primary btn-block">Grant</button>
<button type="submit" name="submit.Login" value="Sign in as different user" class="btn btn-outline-primary btn-block">Sign in as different user</button>
</p>
Try adding a wait. I mean use WebDriverWait to wait for the element to be clickable.
Something like:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div/div/div/div/div/div[2]/form/p[3]/button[1]"))).click();
Also you need to improve your locator.
Absolute XPaths are extremely fragile.
First, try to avoid very long CSS or Xpath expression. You can find the button you need to click on like this:
driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
This code is more readable. If the site will be changed and the element will be moved to another div or span - your code will not work if it relays on the structure of all the divs and spans.
Next, never just click on an element of hover over it in a Selenium test. First check if the element is clickable, then click:
WebElement term = driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(term));
term.click();
I am using Selenium(Java) with Chrome to access the following website:
https://www.ebay-kleinanzeigen.de/m-einloggen.html?targetUrl=/
The Problem is that it always displays a blank page. Here is my Code:
ChromeOptions cap = new ChromeOptions();
cap.setBinary("C:\\Program Files (x86)\\Google\\Chrome Beta\\Application\\chrome.exe");
System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver_win32beta\\chromedriver.exe");
WebDriver driver=new ChromeDriver(cap);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
try {
driver.get("https://www.ebay-kleinanzeigen.de/m-einloggen.html?targetUrl=/");
}catch(Exception e){
System.out.println(e);
}
Every other website I have tried works just fine, but this one doesn't want to show up. I have tried to access this website from Firefox, Chrome and Edge, which also show a blank page. I am using Selenium(3.141.59), ChromeDriver(81.0.4044.20) and Chrome Beta(81.0.4044.62).
Here is the HTML Code when I ispect the website:
It looks like this site can detect Selenium and do not open with it.
You can hide it using Chrome Options. Try adding arguments like this before openning the url:
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-blink-features=AutomationControlled");
ChromeDriver driver = new ChromeDriver(options);
Hope this helped, good luck!
It works with selenium stealth and the following options
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
I experienced the same issue. Check if the page you requested returns a successful HTTP status code like 200. In my case, it returned a 40x error which resulted in a blank page.
I am automating one test case where I clicks on Add key button on bit bucket and open popup as per below screenshot :
Somehow this popup is not opening when I run my script using jenkins on linux AWS.
I am using Selenium Webdriver , Java, Chrome Headless and maven.
Here is my settings in code for chrome headless :
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
chromePath = System.getProperty("user.dir") + prop.getProperty("chromeDriverPath");
System.setProperty("webdriver.chrome.driver", chromePath);
options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1366,768");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-gpu");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--always-authorize-plugins");
options.addArguments("enable-automation");
options.addArguments("--disable-browser-side-navigation");
options.setPageLoadStrategy(PageLoadStrategy.NONE);
driver = new ChromeDriver(options);
Am I missing any other argument which can help me to resolve this issue?
Html Code of button where clicked and open popup :
<div class="buttons">
<button class="aui-button aui-button-primary" id="add-key" resolved="">Add key</button>
</div>
Screenshot of Html :
As per the HTML you have shared to invoke click() on the element with text as Add key you can use the following solution:
It seems that that some of the elements are dynamically generated, ideally instead of PageLoadStrategy.NONE you need to use PageLoadStrategy.NORMAL as follows:
options.setPageLoadStrategy(PageLoadStrategy.NORMAL);
Induce WebDriverWait for the element to be clickable as follows:
CSS_SELECTOR:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.aui-button.aui-button-primary#add-key"))).click();
XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='aui-button aui-button-primary' and #id='add-key'][text()='Add key']"))).click();
webDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.xpath("html/body/div[1]/section[1]/div/div/a/button")).submit();
'View positions' button is not clicking with the above code.What is happening in the web page?
You see the HTML for this page is
So, you can use the CSS selector for this as
WebDriver driver = new FirefoxDriver();
driver.get("https://www.ignitionone.com/company/careers/");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.cssSelector("button.button.teal").click();
And then proceed with doing whatever is necessary. I executed with this in my Python code and it works fine.
Also, you will need to provide the Gecko executable path while calling for the FirefoxDriver()
The way I have done it before is to use the click handler.
driver.findElement(By.cssSelector(".profile-actions .primary_button > span")).click();
I'm sure you could also select the element by xpath rather than CSS in the above line. It's a similar question to this one.
I want to run a program to perform click on google apps icon using Selenium WebDriver but on running the code, it navigates to google product page.
Pease help me to fix this issue.
driver.get("https://www.google.com");
Thread.sleep(3000);
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='gbwa']/div[1]/a")).click();
Try this-
driver.findElement(By.xpath("//*[#id='gbwa']")).click();
or
driver.findElement(By.xpath("//*[#id='gbwa']/div[1]")).click();
Please try to click on the link and not on the div.
Use one of these selectors:
Xpath: //*[#id='gbwa']//a[contains(#href, 'options')]
css: #gbwa a[href*=options]
this works for me:
driver.findElement(By.xpath("//a[contains(#class, 'gb_b') and contains(#class, 'gb_4b')]"));
Hope it helps.
System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
driver.findElement(By.cssSelector("div.gb_Lf")).click();
WebElement ele=driver.findElement(By.xpath("//iframe[contains(#id,'I0')]"));
driver.switchTo().frame(ele);
driver.findElement(By.xpath("//*[text()='YouTube']")).click();