I have a Angular SPA application which is having several dialog windows:
Full code: https://pastebin.com/81ikb5gE
<mat-dialog-container aria-modal="true" class="mat-dialog-container ng-tns-c18-93 ng-trigger ng-trigger-dialogContainer ng-star-inserted" tabindex="-1" id="mat-dialog-12" role="dialog" style="transform: none;"><!----><mobileweb-inquiry-menu-dialog _nghost-shn-c52="" class="ng-star-inserted"><mobileweb-client-area _ngcontent-shn-c52="" id="clientarea" _nghost-shn-c3="">
.....
</mat-dialog-container>
I facing the following issue: Into this dialog I have the same html tag ids. Sometimes I have the same ids 4 times. The application is working fine bug I get always the first id and the rest are not found.
I use this code to open the dialog window:
WebDriverWait webDriverWait = new WebDriverWait(driver, 25);
System.out.println("Click on Button " + name + " using id locator " + buttonId);
WebElement webDriverElement = webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id(buttonId)));
webDriverElement.click();
Is it possible to isolate the driver instance of WebDriver only to the elements of the current active dialog?
For example is it possible to get all elements into the dialog window and cut all of the rest html elements?
Are there any other solutions?
The application is working fine bug I get always the first id and the rest are not found :-
for this issue you can try to use xpath axes or xpath indexing.
let's say you have a xpath like this :
//input[#class='some-class']
that represent 4 items, you can perfrom xpath indexing like this :
(//input[#class='some-class'])[1]
or
(//input[#class='some-class'])[2]
and for this :
Is it possible to isolate the driver instance of WebDriver only to the elements of the current active dialog? For example is it possible to get all elements into the dialog window and cut all of the rest html elements?
I would say to have a locator that represent that particular model dialogue.
and you can call getAttribute('innerHTML') and should represent the particular HTML content.
Xpath axes
Related
I tried a lot to locate elements on this page with this link
ALL I want to do is to select "1 queen bed" or "1 double large bed" and then select amount from the drop down list then press, I'll reserve button.
But I totally failed trying all of these:-
using Action --> moveToElement --> perform()
using JS Execution --> scrollToView, scrollBy(0,100) for example to make the page scroll down
using all types of locators (ID, xPaths, cssSelectors, names) but always an error message that Expected condition failed: waiting for visibility of element located by By.xpath- By.id .. etc.
tried to search for an iFrame but I didn't find but I think there are some but not related to the locators I want
Thread.sleep() to wait for a time if the element is not loaded on the web page.
I am using Selenium JAVA TestNG
so in the page I write this function:-
Page class I write this function :-
private By btnBed = By.xpath("(//i[#class='bicon bicon-double'])[1]");
public void clickBed(){
// JavascriptExecutor exe = (JavascriptExecutor)driver;
// exe.executeScript("window.scrollBy(0,1000)");
click(btnBed);
}
In my Test I write this:-
hotelPage.clickBed();
Error message:-
Expected condition failed: waiting for visibility of element located by By.xpath
The problem was that this page was opened in a new tab so the code can't locate any elements on the screen.
So I just switch tabs, the code works fine.
ALL I want to do is to select "1 queen bed" or "1 double large bed"
I inspecting the DOM on the link you provided, I see that it is radio button that you might want to select. Try locate the radio element button something like this
<input type="radio" value="1" name="bedPreference_78883120" data-bed-type="">
Locate by name
By.name("bedPreference_78883120")
then perform the click.
In AliX, this page for example https://www.aliexpress.com/item/32956185908.html
how do you pick the particular country and colour?
For example, following js configure as country:
"skuPropertyValues":[
{"propertyValueDisplayName":"China","propertyValueId":201336100,"propertyValueIdLong":201336100,"propertyValueName":"China","skuPropertySendGoodsCountryCode":"CN","skuPropertyTips":"China","skuPropertyValueShowOrder":2,"skuPropertyValueTips":"China"},
{"propertyValueDisplayName":"GERMANY","propertyValueId":201336101,"propertyValueIdLong":201336101,"propertyValueName":"GERMANY","skuPropertySendGoodsCountryCode":"DE","skuPropertyTips":"GERMANY","skuPropertyValueShowOrder":2,"skuPropertyValueTips":"GERMANY"},
{"propertyValueDisplayName":"SPAIN","propertyValueId":201336104,"propertyValueIdLong":201336104,"propertyValueName":"SPAIN","skuPropertySendGoodsCountryCode":"ES","skuPropertyTips":"SPAIN","skuPropertyValueShowOrder":2,"skuPropertyValueTips":"SPAIN"},
{"propertyValueDisplayName":"Russian Federation","propertyValueId":201336103,"propertyValueIdLong":201336103,"propertyValueName":"Russian Federation","skuPropertySendGoodsCountryCode":"RU","skuPropertyTips":"Russian Federation","skuPropertyValueShowOrder":2,"skuPropertyValueTips":"Russian Federation"}]},
I'm not sure how to pick one through JavaScript or am I looing at the wrong thing, would it CS and picking up a div tag?
You have to enable js support and wait after retrieving the page until all the javascript is done. The js code will generate div's from you code above (use page.asXML() to get an idea of the page fro the viewpoint of HtmlUnit.
If the div's are there you can click() on them - like on any other html element. This click should trigger the same js code like in real browsers.
To find the div elements please have a look at https://htmlunit.sourceforge.io/gettingStarted.html; there are many different options listed.
Like #RBRi said, use the click method on the element. To choose the element (DIV in this case) you can use the getbyxpath method:
Starting from the page you can access using the XPATH of the element. The XPATH can be easily obtained using the browser inspect tool and copying the full XPATH of the div (right click over the div and use the copy option). For the purpose of your URL, "CHINA" element has the XPATH: /html/body/div[6]/div/div[2]/div/div[2]/div[7]/div/div[1]/ul/li[1]/div/ "GERMANY": /html/body/div[6]/div/div[2]/div/div[2]/div[7]/div/div[1]/ul/li[2]/div/span
page1 = webClient.getPage("https://www.aliexpress.com/item/32956185908.html");
// get china div using xpath
element = ((HtmlElement)page1.getByXPath("/html/body/div[6]/div/div[2]/div/div[2]/div[7]/div/div[1]/ul/li[1]/div/").get(0));
// Click over china
element.click();
webClient.waitForBackgroundJavaScript(10000);
If you want to iterate over the list of countries you can use the same approach, but this time copy the XPATH of the div that contains all the countries, get that element, and from this element then iterate by getting the divs. In this case, you can use the attribute "class" to get those elements:
page1 = webClient.getPage("https://www.aliexpress.com/item/32956185908.html");
element = ((HtmlElement)page1.getByXPath("/html/body/div[6]/div/div[2]/div/div[2]/div[7]/div/div[1]/ul").get(0); // Get the div that contains all countries
List<HtmlElement> elements = element.getElementsByAttribute("div", "class", "sku-property-text");
Then you can iterate over the list of elements and click the one you prefer.
:
choosenElement.click();
:
Click button via class because it has no ID . Or via value?
tried className , cssSelector , partialLinkText and LinkText but sadly did not work clicking the save button
System.out.println("Succesful in Saving Product ");
WebElement save = driver.findElement(By.className("bttn-positive save-button"));
save.click();
Should be able to click save button
we can not use the multiple class name in the className locator. So, you can use the XPath locator with the multiple class name as below (//input[#class='bttn-positive save-button'])
Code:
System.out.println("Succesful in Saving Product ");
WebElement save = driver.findElement(By.xpath("//input[#class='bttn-positive save-button']"));
save.click();
You can't pass multiple classnames while using driver.findElement(By.className("bttn-positive save-button")) and doing so you will face Invalid selector: Compound class names not permitted error.
To click() on the green button with text as Save you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.bttn-positive[value^='Save'][type='button']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(#class, 'bttn-positive') and starts-with(#value, 'Save')][#type='button']"))).click();
Try save.submit();
Submit buttons are used to submit the entire form to the server. We can either use the click () method on the web element like a normal button as we have done above or use the submit () method on any web element in the form or on the submit button itself.
In this case "save.click()" will be work, but some time to save any product on any app like eCommerce or Banking domain it will not working properly & one more important think click () cause a new page to load, this method will attempt to load the page . So better to use "save.submit()" if the current elements is form Or with in the form the this will be submitted. As up ur requirements submit () one is the better option.
I'm trying to get the image which has the word "Collector" in its title and click on it.
This is the html code for the image and its link:
<a href="javascript:*command*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
<img src="*unusable link because it's only valid for the specific page*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
The <a> and <img> tags are nested in a table cell and some divs. I didn't write the html code so don't yell at me if it's ugly :p
Here is the java code where I try to do it:
WebElement trafficLight = driver.findElement(By.xpath("//img[contains(#title,'Collector')]"));
trafficLight.click();
The error I get is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//img[contains(#title,'Collector')]"}
I'm pretty sure the xpath is ok so I don't think that's the issue.
As the img WebElement is within a frame, you will need to switch focus to that frame before you perform any action on that WebElement. You can do that using WebDriver's switchTo() method like so:
driver.switchTo().frame(frameLocator);
The frame locator can be either its (zero-based) index, name or id attribute, or a previously located WebElement.
Once you have switched focus to the required frame, you should then be able to interact with the WebElement using the same code in your initial post.
Please try this. It will resolve your problem.
WebElement frameSwitch = driver.findElement(By.xpath("Give iframe Xpath location"));
driver.switchTo().frame(frameSwitch); //Switch control to iframe.
//Perform your steps (I.e Click on Image)
driver.findElement(By.xpath("//img[contains(#title,'Collector')]")).click();
driver.switchTo().defaultContent(); //Come out of iframe.
I am trying to write some UI test cases for an SAP-webUI (web based) application. After login, it shows the dashboard ( Workcenter's ) screen.
Now the problem is, I am able to open the page, enter U/N, Pwd and login through Selenium. After i press the "Login" button the URL changes and the page got redirected/refreshed.
E.g. URL before login : https://a/b/c/d/e/f/g.htm?sap-client=001&sap-sessioncmd=open
E.g. URL after successful Login : https://a/b(bDsdfsdsf1lg==)/c/d/e/f/g.htm
After this im unable to perform any action or press any link in any part of the page. I tried with all the possible attributes ( css, xpath, id ). Webdriver was unable to find any element on the page. It shows the error "No element found" alone.
I am using java with Selenium Web Driver.
Please find the html structure of the webpage below
<html><body><div><div><iframe>#document<html><head></head><frameset><frameset><frame>#document<html><head></head><body><form><div><div><table><tbody><tr><td><div><ul><li><a id=abcdef></a></li></ul></div></td></tr></tbody></table></div></div></form></body></html></frame></frameset></frameset></html></iframe></div></div></body></html>
Actually i want to click a linkmenu "abcd", which is inside iframe and frame as shown in the below HTML code
<html><head></head><body><iframe name=a1><html><head></head><frameset><frameset name=fs1><frame name=f1><html><head></head><body><table><tbody><tr><td><ul><li><a id=abcdef>
I tried the below code as well.
driver.switchTo().frame("a1");
driver.findElement(By.id("abcd")).click();
OR
driver.findElement(By.xpath("//*[#id='abcd']")).click();
After using the above code, still im getting the error "No such element"
Kindly suggest
Regards,
Siva
Do it this way...
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#name='a1']"))); // switching to iframe
followed by
driver.switchTo().frame("f1"); // switch to frame
and then your desired action...
driver.findElement(By.id("abcd")).click();
This is because of the iframe. You need to switch to it first:
driver.switchTo().frame(0);
driver.findElement(By.id("abcdef")).click();
where 0 is a frame index.
See doc on implicit wait here
I guess you should do a implicit wait until your chosen element is available
modify these code to suit your chosen element:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));