How to write xpath for the ember dropdown.
<ul id="ember-power-select-options-ember2473" class="ember-power-select-options ember-view" aria-controls="ember-power-select-trigger-ember2473" role="listbox"> <li class="ember-power-select-option" aria-selected="false" aria-current="false" data-option-index="0" role="option">Option A
Since the ember id changes, how can i write xpath??
With xpath:
//ul[contains(#id, 'ember-power-select-options-ember')]
With css:
ul[id*='ember-power-select-options-ember']
Other css:
ul.ember-power-select-options[role=listbox]
The value of the value of the id attribute will keep changing dynamically, everytime you access the AUT(Application Under Test). Hence to interact / click the dropdown you need to construct dynamic Locator Strategies as follows:
cssSelector:
ul.ember-power-select-options.ember-view[id^='ember-power-select-options-ember']
xpath:
//ul[starts-with(#id, 'ember-power-select-options-ember') and #class='ember-power-select-options ember-view']
References
You can find a couple of relevant detailed discussions in:
How to click on the ember.js enabled button using Selenium and Python
Selenium - Finding element based on ember
Related
I'm trying to automate some web testing using Kantu and Selenium. The page is using pq-select ParamQuery to generate select menus, but neither Kantu or Selenium can see them in the page.
I'm guessing my best bet is to use xpath to locate them, but I'm not too sure on how to do that. The HTML for the select menu is:
<td style="white-space: nowrap " aria-describedby="df230254-d8a5-4ba1-9950-58d26145d5a9" role="gridcell" data-container-for="section1" id="sectiongrid_active_cell" class="">
If I could use the data-container-for that would be the best I think, but I'm not sure how to get that via xpath.
As per the HTML you have shared you can use either of the following solutions:
XPath - 1:
//td[#id='sectiongrid_active_cell' and #data-container-for='section1']
XPath - 2:
//td[#id='sectiongrid_active_cell' and #data-container-for='section1'][#role='gridcell']
Note: The element is a ParamQuery select element which is being converted to theme ready jQueryUI widget, you have to induce WebDriverWait for the element to be visible/interactable.
I have a "Logout" button in a user portal that I don't get to grab.
The frontend is knockout.js and the framework for testing is Selenium.
Now I can get many elements via the ID, but not this logout button.
A lot of help on the Internet is already outdated and I just can't get any further.
<button name="logout" class="btn btn-primary" data-action="userLogout" data-bind="click: logout" data-i18n="app:modules.localization.generals.button.logout">Abmelden</button>
This is the source code of the button.
For example, if I execute this code
webDriver.FindElement(By.Id("logout")).Click();
I get this message
Message: Test method MyFirstAutomaticTest.UnitTest1.StartPage threw exception:
System.InvalidOperationException: Cannot click on element (MoveTargetOutOfBounds)
Can someone help me with that?
As per the HTML you have shared to click() on the element with text as Abmelden you can use either of the following solutions:
cssSelector:
webDriver.FindElement(By.CssSelector("button.btn.btn-primary[data-action='userLogout']")).Click();
xpath:
webDriver.FindElement(By.XPath("//button[#class='btn btn-primary' and #data-action='userLogout']")).Click();
Note: As the element is an Angular element you have to induce WebDriverWait for the desired element to be clickable.
You have bind name attribute as ID,
Try this,
webDriver.FindElement(By.name("logout")).Click();
Try with xpath
webDriver.FindElement(By.Xpath("//button[text()='Abmelden']")).Click();
I am beginner in selenium.
How to locate the element pagelink in selenium?
<li>
<t:pagelink page="NewCustomer" >New Customer</t:pagelink>
</li>
I used this :
driver.findElement(By.partialLinkText("NewCustomer")).click();
but it didnt work for me.
Any suggestions?
Thanks;
Look at the HTML source in your browser. You'll see that the Tapestry pagelink component renders itself as an ordinary HTML hyperlink, perhaps <a href="/NewCustomer">. That's what Selenium must look for. Something like this:
//*[local-name()='a' and #href='/NewCustomer']
But a better strategy might be to add a CSS class name or id other attribute specifically to make it easier for Selenium to find the element:
<li>
<t:pagelink page="NewCustomer" class="myNewCustomerLink">New Customer</t:pagelink>
</li>
//*[local-name()='a' and #class='myNewCustomerLink']
You can use driver.findElement(By.linkText("New Customer").click(), this will work.
I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.
<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>
I tried below codes but did not work and still getting the Unable to locate element error.
driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();
I have to click the homebutton. Any help would be much appreciated
I don't know why By.className("HomeButton") didn't work but you have errors in the other two.
In driver.findElement(By.xpath("//a[#onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();
driver.findElement(By.xpath("//span[#onclick='javascript:onBtnHomeClick();']")).click();
If you want to use onBtnHomeClick() use contains
driver.findElement(By.xpath("//span[contains(#onclick, 'onBtnHomeClick')]")).click();
Or
driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();
And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
You simply need the correct locator IF your element will be eventually visible.
Xpath = "//span[contains(#class,'HomeButton') and contains(#onclick,'onBtnHomeClick')]"
Add wait as needed above exanmple, that should work.
I am stuck here. I am trying a write Xpath for the following. I am trying to click "Browse". The ID is changing every time.
I have tried this as my Xpath:
//*[#id='ext-gen43']/em/span/span
<a id="ext-gen43" class="x-tab-right" href="#">
<em class="x-tab-left">
<span class="x-tab-strip-inner">
<span class="x-tab-strip-text ">Browse</span>
</span>
</em>
</a>
XPATH WRITING PLUGINS :
Inorder to write xpath's by yourself you must first install firebug
and firepath plugins, they are the plugins available for Firefox
browser.
You can also install xpath checker which is another plugin available for firefox. It's a awesome plugin because you can actually
see the UI elements in the xpath checker as you are writing the
xpath.
EXAMPLE :
Inorder to write xpath's by yourself you must following the child and parent hierarchy available in the HTML tab in firebug.
STEPS :
Hover on the element you want to write xpath for using the small blue arrow available in the firebug toolbar in the top left corner.
Now click on the element, you would observe that the tag for the respective element in the firebug is highlighted.
Say your tag looks like :-
<label> class="control-label col-sm-3" for="name"> Your Name <label>
So the xpath for respective tag would be :-
//label[#class="control-label col-sm-3"]
So the above xpath specifies //parent tag which is label and into the bracket we should specify the locator, locator can be id, class, name anything.
And in your case the xpath would be :-
//span[#class="x-tab-strip-text"]
Have You tried copy xpath for given element in your browser(check element -> copy xpath)?
Then delete id and check once again.
It should be easy if you know how to select element by xpath with given class.
Try to use firepath to get xpath. It`s addon for FireFox https://addons.mozilla.org/uk/firefox/addon/firepath/