Here is the image button source:
<img src="/NU/b-nb.gif" border="0" name="New Business" label="New Business" xpath=".">
I'm trying to click on the image button on IE using selenium webdriver but I'm unable to find the element.
You can select element by name or some other approach & simple call the click event using click() method.
WebElement myImgBtn= driver.findElement(By.name("New Business"));
myImgBtn.click();
As, you have this element in other html frame. So you have to switch the driver to that iframe before selection of element.
e.g.
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#id,'your_iframe_id')]")));
WebElement myImgBtn= driver.findElement(By.name("New Business"));
myImgBtn.click();
driver.findElement(By.xpath("//img[#name='New Business']")).click();
Related
I am trying to automate test script and ran upon the pseudo element which seems to not clickable using normal element finders. The element to be clicked is a field after which the checkbox pop which is to be clicked again.
<span class="mat-select-placeholder mat-select-min-line ng-tns-c156-64 ng-star-inserted">
:before==$0
</span>
enter image description here
I have tried to click it using Actions method and JavascriptExecutor but was not able to. Please help.
This is the codes that I have tried.
driver.findElement(By.xpath("//span[#class='mat-select-placeholder mat-select-min-line ng-tns-c211-60 ng-star-inserted']").click();
I am having two issues in clicking two buttons during my automation
First : A View button which have the following Details
<button class="veiw-btn" data-toggle="collapse" data-target=".toggle-content1" aria-expanded="false" aria-controls="toggle-content1" ng-click="gotoAnchor(flightResult.FlightId)">VIEW</button>
There are several VIEW buttons on the page but they are differentiated with the toggle-content (they have numbers 1,2,3,4) I just need to select the first one and click on it
Second :
After clicking the View i want to also click the Continue Button with the following code
<div class="text-center">
<button class="flight-book-btn" type="button" ng-click="select(false, flightResult);">
<span>Continue</span>
</button>
</div>
My Major issue is the First Code but if i can get help with both i will be glad . I have not been able to click on the First VIEW Button
i have tried some samples online but they have not worked for me
I expect to be able to click the VIEW and Continue Buttons
CODE:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
log.debug("Fastest Sort Available ");
log.debug("Now about to click VIEW Airline Details ");
// driver.findElement(By.xpath("//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']")).click();;
driver.findElement(By.cssSelector("button[class=\"veiw-btn\"][data-target='.toggle-content1'']")).click();
Try using the css selector or Xpath
CSS:
driver.FindElement(By.CssSelector("button[class="veiw-btn"][data-target='.toggle-content1']").Click();
xpath:
driver.FindElement(By.XPath("//button[#class='veiw-btn'][#data-target='.toggle-content1']").Click();
You can use this xpath to click on VIEW button :
//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']
For clicking on Continue button you can try with this xpath :
//span[text()='Continue']/parent::button[#class='flight-book-btn']
I have fixed this by using this
WebElement element= driver.findElement(By.xpath("//button[text()='VIEW' and #data-target='.toggle-content1' and #aria-controls='toggle-content1']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
This way I tried to switch to the frame:
driver.switchTo().frame(driver.findElement(By.xpath(("//iframe[contains(#name,'vfFrameId')]//following::iframe[2]"))));
There are three iframes on the complete html code and I am trying to switch to the third one and I am able to the locate that as well using xpath but after running the script driver is not able to locate that and throwing an exception.
Exception:
no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[contains(#name,'vfFrameId')]//following::iframe[2]"}
HTML:
<div class="windowViewMode-normal oneContent active forcePageHost" data-aura-rendered-by="1330:0" data-aura-class="forcePageHost">
<div class="oneAlohaPage" data-aura-rendered-by="1335:0" data-aura-class="oneAlohaPage">
<force-aloha-page data-data-rendering-service-uid="240" data-aura-rendered-by="1338:0" force-aloha-page_aloha-page="">
<div class="slds-template_iframe slds-card" force-aloha-page_aloha-page="">
<iframe height="100%" width="100%" scrolling="yes" allowtransparency="true" id="vfFrameId_1536141890078" name="vfFrameId_1536141890078" allowfullscreen="true" force-aloha-page_aloha-page="" allow="geolocation *; microphone *; camera *" title="Deploy Data Set"></iframe>
</div>
</force-aloha-page>
</div>
</div>
Here is how you can find the index of each iframe element
List <WebElement> options= driver.findElements(By.tagName("iframe"));
for(WebElement e: options) {
String index=e.getAttribute("index");
System.out.println("Index for: "+e.getAttribute("name")+" is: "+index);// to print out name attribute and index
}
If you are switching from iframe to iframe that don't work all the times. You may need to switch to default content from iframe(1) first and then switch to iframe(2)
driver.switchTo().frame(your locator for 1st iframe here);// to switch to 1st iframe
// your action here
driver.switchTo().defaultContent();
driver.switchTo().frame(your locator for 2nd iframe here); // to switch to 2nd iframe
I am working from your comment:
so you can see the code above where you can see that there are three iframes available on the browser. what happens that i click on one link then to interact with the elements i have to switch to the iframe and here i am able to switch but after clicking on an element ,i am getting navigated to the next page and there is another iframe introduced where i am not able to locate the iframe
You need the following sequence of events:
Switch to your iframe. From this point, your driver instance is pointing at this iframe!
Click on the element that transitions to the next page.
Your driver instance is still pointing at the iframe from step 1! You need to reset it to the top document using: driver.switchTo().defaultContent();.
Only now can you switch to the new iframe on the page you just navigated to.
As per the text based HTML and the HTML snapshot you have shared, it is not evident if there are multiple iframes available. So considering this <iframe> as a unique iframe you need to induce WebDriverWait while switching to the <iframe> as follows:
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='vfFrameId_'][name^='vfFrameId_'][title='Deploy Data Set']")));
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#id,'vfFrameId_')][starts-with(#name,'vfFrameId_')][#title='Deploy Data Set']")));
Incase there are multiple iframes available with the same attributes which we have used above you can use the following solution:
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//div[#class='windowViewMode-normal oneContent active forcePageHost' and #data-aura-class='forcePageHost']//following::iframe[1]")));
I am trying to select a frame in a dojo page with the selenium method shown below is failing
driver.switchTo().frame(target);
I have passed title attribute as my target value.
For the target i do not have id or name attributes, how could i pass the target value for the below element.
<iframe frameborder="0" title="Universal Line Template Configuration" style="width:100%;height:100%;" src="/ucmadmin/ccmadmin-latest?name=universalLineTemplate&key=a91ffd45-a34f-bc90-c159-1bef9e7c9921&permission=3">
When i check by clicking a button in a dojo page, the selenium IDE is showing two operations for a single click, as shown below
click id=g_btnExpandAll_label
click name=g_btnExpandAll
Is anything to be done apart from the line driver.switchTo().frame(target);?
You need to locate the frame first. For example, by xpath relying on the title tag:
WebElement frame = driver.findElement(By.xpath('//iframe[#title="Universal Line Template Configuration"]'));
The use it as a target:
driver.switchTo().frame(frame);
See also:
How to switch between frames in Selenium WebDriver using Java
In testing a simple website, I find that when using the Firefox webdriver I am unable to get a Javascript calendar window to appear when the button is clicked. I am able to spawn the window in the Selenium IDE but when I run the Java code, the click is registered, but no window is spawned.
The Code I am using to click the Javascript element is:
WebElement element = driver.findElement(By.cssSelector("img[alt=\"Pick a date\"]"));
element.click();
Additional info: The 'cal.gif' image is also not shown when using the webdriver. The problem is NOT switching to the calendar window or selecting and element within, it is simply getting the window to spawn at all.
This is the website under test: Parking Meter
I have searched quite a bit for the solution, either I am not searcher the right keywords or I am missing something obvious, any help would be appreciated.
edit: HTML code for the JS calendar:
<a href="javascript:NewCal('EntryDate','mmddyyyy',false,24)"
<img height="16" width="16" border="0" alt="Pick a date" src="cal.gif"></img>
</a>
What your doing here is wrong. You need to click on the a tag not on the img tag.
Look at the below code, which is working fine for me:
#Test
public void testSO() throws Exception
{
driver.get("http://adam.goucher.ca/parkcalc/index.php");
Thread.sleep(2000);
driver.findElements(By.tagName("a")).get(0).click();
}
Change the index to 0 or 1 accordingly.
Id be using the JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("<JavaScript.click;");