Selenium webdriver can't find xpath - selenium

element code:
<div class="col col-2"><div class="v-input v-input--is-focused theme--light v-text-field v-text-field--is-booted v-select primary--text"><div class="v-input__control"><div role="button" aria-haspopup="listbox" aria-expanded="false" aria-owns="list-65" class="v-input__slot"><div class="v-select__slot"><label for="input-65" class="v-label v-label--active theme--light primary--text" style="left: 0px; right: auto; position: absolute;">Narystė</label><div class="v-select__selections"><input id="input-65" readonly="readonly" type="text" aria-readonly="false" autocomplete="off"></div><div class="v-input__append-inner"><div class="v-input__icon v-input__icon--append"><i aria-hidden="true" class="v-icon notranslate material-icons theme--light primary--text">arrow_drop_down</i></div></div><input type="hidden" value="[object Object]"></div><div class="v-menu"><!----></div></div><div class="v-text-field__details"><div class="v-messages theme--light primary--text"><div class="v-messages__wrapper"></div></div></div></div></div></div>
in screen you can see, that entered xpath is only one.
Here is code, where i have entered the same xpath, but getting error like this:
private static final By naryste = By.xpath("//*[contains(#class,'v-label v-label--active theme--light primary--text') and contains(text(),'Narystė')]");
#Step("Pasirenkame juridinio asmens organizaciją iš reikšmių sąrašo")
public createOrganization selectMembership() {
button.click(naryste);
return this;
}
Selenium webdriver can't find this xpath:
Find element :By.xpath: //*[contains(#class,'v-label v-label--active theme--light primary--text') and contains(text(),'Narystė')]
P.S. the same problem if i choose other elements
AND this (selenium can't find)

To locate the element you can use the following Locator Strategy:
Using xpath:
By.xpath("//label[starts-with(#for,'input') and contains(.,'Narystė')]");
PS: Do add some waits before invoking the click()

Related

Unable to automate a click using Python and Selenium

I'm trying to automate adding requirements to a TestLink database. I'm running into an issue trying to click on this anchor/span.
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span>
</a>
Here is the section of Python code with things I've tried:
anchor = browser.find_element_by_xpath('//a[contains(#href, "REQ_SPEC_MGMT")]')
span = anchor.find_element_by_xpath('.//span')
anchor.click() # Doesn't work
span.click() # Doesn't work
browser.execute_script("arguments[0].click();", anchor) # Doesn't work
browser.execute_script("arguments[0].click();", span) # Doesn't work
I don't get any errors, but I still don't get the page that appears when I manually click on the link. I verified that I'm finding the correct anchor/span by dumping out the properties so I know I have the correct elements. I've also tried long pauses just to make sure that the element is clickable before I try it. Any ideas on what I am doing wrong? Thanks!
Update - Here's a larger section of the HTML:
<div id="tree_div" style="overflow:auto; height:100%;border:1px solid #c3daf9;" class=" x-panel x-tree">
<div class="x-panel-bwrap" id="ext-gen12"><div class="x-panel-body x-panel-body-noheader" id="ext-gen13" style="overflow: auto;">
<ul class="x-tree-root-ct x-tree-arrows" id="ext-gen14">
<li class="x-tree-node"><div ext:tree-node-id="17472" class="x-tree-node-el x-unselectable x-tree-node-expanded" unselectable="on" id="extdd-1">
<span class="x-tree-node-indent"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-end-minus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-2">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:TPROJECT_REQ_SPEC_MGMT(17472)" tabindex="1">
<span unselectable="on" id="extdd-3">ProjTasks (0)</span>
</a>
</div>
<ul class="x-tree-node-ct" style="">
<li class="x-tree-node">
<div ext:tree-node-id="17473" class="x-tree-node-el x-unselectable folder x-tree-node-collapsed" unselectable="on" id="extdd-4">
<span class="x-tree-node-indent"><img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-icon"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-plus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-5">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span></a></div><ul class="x-tree-node-ct" style="display:none;">
</ul>
</li>
At first, try to use implicit selenium wait, to make sure that all your elements is download.
anchor = WebDriverWait(browser,30).until(EC.element_to_be_clickable((By.XPATH, '//a[contains(#href, "REQ_SPEC_MGMT")]')))
span = WebDriverWait(anchor,30).until(EC.element_to_be_clickable((By.XPATH, './/span')))
If it does'n work fine, try to execut js script in href:
browser.execute_script("javascript:REQ_SPEC_MGMT(17473)")
or
browser.execute_script(anchor.get_attribute("href"))
I figured out the issue. Some of the characters I was passing to execute_script were being dropped by my vnc software (Python Selenium script drops keys but only when used on a VNC).
To click() on the element with text as 0:Project-0 (0) element is an JavaScript enabled element so to click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.x-tree-node-anchor[href*='REQ_SPEC_MGMT']>span[id^='extdd-']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='x-tree-node-anchor' and contains(#href, 'REQ_SPEC_MGMT')]/span[starts-with(#id, 'extdd-') and text()='0:Project-0 (0)']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How does one use Selenium Webdriver : to choose text from a dynamic combobox with tag <aria-activedescendant>?

This is the html:
<select select2="" id="id_event_operator" value.bind="model.operator"
style="width:100%;display:block" class="au-target select2-hidden-accessible"
au-target-id="134" data-select2-id="id_event_operator" tabindex="-1" aria-
hidden="true"> <option model.bind="op.id" class="au-target" au-target-id="135"
data-select2-id="2">--- None ---</option><option model.bind="op.id" class="au-
target" au-target-id="135" data-select2-
id="11">liveeventuplynktest01#test.com</option><option model.bind="op.id"
class="au-target" au-target-id="135" data-select2-
id="12">uplynkcmsseleniumtestaccount#test.com</option><!--anchor--> </select>
<span class="select2 select2-container select2-container--default select2-
container--below select2-container--open" dir="ltr" data-select2-id="1"
style="width: 100%;"><span class="selection">
<span class="select2-selection
select2-selection--single" role="combobox" aria-haspopup="true" aria-
expanded="true" tabindex="0" aria-labelledby="select2-id_event_operator-
container" aria-owns="select2-id_event_operator-results" aria-
activedescendant="select2-id_event_operator-result-q5d6-
liveeventuplynktest01#test.com">
<span class="select2-selection__rendered"
id="select2-id_event_operator-container" role="textbox" aria-readonly="true"
title="--- None ---">--- None ---</span>
<span class="select2-selection__arrow"
role="presentation"><b role="presentation"></b></span></span></span>
<span class="dropdown-wrapper" aria-hidden="true"></span></span>
The value of the "aria-activedescendant" keeps on changing as I hover over the elements in the combobox.
How can I grab the value in combobox and set it to the textbox element.Clicking on the "class="select2-selection select2-selection--single" element is not setting the textbox value when I am doing it through the code, but it works if I manually do it from UI.
This is how the web page looks like
This is my code:
public class LiveEventsPage extends PageObject {
#FindBy(xpath = "/html/body/router-
view/div[4]/div/div[2]/div/form/ul/li[1]/span/span[1]/span/span[2]")
private WebElement eventOperatorDropDownButton;
public void setEventOperator(String dropDownElem){
waitForElemToBeClickable(eventOperatorDropDownButton).click();
driver.findElement(By.xpath("//span[contains(#aria-
activedescendant,'liveeventuplynktest01#test.com')]")).click();
}
}
Locate with this Xpath :
if(driver.findElement(By.xpath("//span[#aria-labelledby='select2-id_event_operator-container']")).isDisplayed())
{
String getData = driver.findElement(By.xpath("//span[#aria-labelledby='select2-id_event_operator-container']")).getAttribute("aria-activedescendant");
}

How to automate the bootstrap fileupload upload control using webdriver

I want to automate the file uploading process which is using a file upload control built in bootstrap.
I am doing the same using webdriver.
Below is my code, but unfortunately it is not working:
element=driver.findElement(By.xpath("//[#id='upload']/fieldset/div[2]/input[1]"));
element.sendKeys(pathToFile);
It is giving an element not visible error.
Here is the example of the bootstrap fileupload control which I am trying to automate-
Via JavaScript:
on this URL http://markusslima.github.io/bootstrap-filestyle/
Please see below style-
$(":file").filestyle({icon: false});
Ok. i think i solved it.
WebElement fileInput = driver.findElement(By.id("document"));
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.id("document"));
js.executeScript("arguments[0].setAttribute('style', 'left:30px')",
element);
fileInput.sendKeys(fileName);
the bootstrap-filestyle.js hide a input element so you have to move it to the visible area and then set it in the standard way.
so much trouble for such a simple solution.
Here is my original html code:
<span id="documentUpload">
<input type="file" id="document" name="document" class="notMandatory" onkeypress="return noenter(event)" tabindex="-1" style="position: absolute; left: -9999px;">
<div class="bootstrap-filestyle" style="display: inline;" tabindex="0">
<input type="text" class="input-xlarge" disabled="" autocomplete="off">
<label for="document" class="btn"><i class="icon-folder-open"></i> <span>Upload</span></label>
</div>
</span>

Selenium WebDrivers -Not able to select dynamic check box which has no name

<div id="checkboxfield-3844" class="x-field x-form-item x-field-default x-form-cb-checked x-form-dirty" style="width: 492px;">
<label id="checkboxfield-3844-labelEl" class="x-form-item-label x-form-item-label-left" style="margin-right:5px;width:200px;" for="ext-gen6460">Is Stitching Point:</label>
<div id="checkboxfield-3844-bodyEl" class="x-form-item-body x-form-cb-wrap" role="presentation" style="width: 287px;">
<input id="ext-gen6460" class="x-form-field x-form-checkbox" type="button" hidefocus="true" autocomplete="off" aria-checked="true" aria-invalid="false" role="checkbox" aria-describedby="checkboxfield-3844-errorEl" style="-moz-user-select: text;" data-errorqtip="">
</div>
<div id="checkboxfield-3844-errorEl" class="x-form-error-msg" style="display:none"></div>
<div class="x-clear" role="presentation"></div>
</div>
This is my Div , out of it iam not able to select the checkbox since there is no name .Need some help on this
Assuming there is only one 'label tag' with innerHTML/text as "Is Stiching Point:", the below xpath will point to the checkbox:-
driver.findElement(By.xpath("//label[.='Is Stitching Point:']/..//input[#role='checkbox']")).click();
NOTE: Above is a java code. In case you are using a different language binding, you can refer this to employ the same thing.
Edit
Below is the alternative way using JavascriptExecutor:
WebElement element = driver.findElement(By.xpath("//label[.='Is Stitching Point:']/..//input[#role='checkbox']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);

NoSuchElementException with unknown cause using Selenium

While writing selenium testcases for a webapplication I'm having trouble with the xpath selector. The element of the HTML-code which should be clicked on by Selenium is the following:
<a title="Voeg een vak toe" href="#" onclick="javascript:$.colorbox({width:818,href:'/olo-cleanjump/profiel/addVakForm'}); return false;">
<p class="add">
<img class="add-icon" src="/olo-cleanjump/static/images/icon_add.png"/>
Voeg vak toe
</p>
</a>
The Selenium IDE plugin for firefox gives me the following selenium code for this:
driver.findElement(By.cssSelector("p.add")).click();
The addVakForm javascript function that is called by this link opens a colorbox with the following HTML (I shortened it, there are around 30 similar div's with class "lesboek_popup") inside:
<div id="cboxLoadedContent" style="display: block; width: 776px; overflow: auto; height: 653px;">
<div id="profielpagina_add">
<h2>Voeg een vak toe aan je profiel</h2>
<div class="lesboek_popup">
<a class="content" href="/olo-cleanjump/profiel/addvak/120776">
<img src="" alt="">
</a>
<p class="caption">
Engels
</p>
</div>
<div class="lesboek_popup">
<a class="content" href="/olo-cleanjump/profiel/addvak/120786">
<img src="" alt="">
</a>
<p class="caption">
Biologie
</p>
</div>
</div>
For the test I want to open the 'Biologie' link. Selenium IDE got me the following selenium code to do this
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
to select this biology link element.
Based on this I wrote the following testcase:
Test
public void testAddRemoveVak() throws Exception {
this.get("");
// vak 1 toevoegen
driver.findElement(By.cssSelector("p.add")).click();
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
// vak 2 toevoegen
driver.findElement(By.cssSelector("p.add")).click();
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
assertEquals(driver.findElements(By.xpath("//li[#class='vak']")).size(), 2);
// vak 2 verwijderen
this.get("profiel/removevak/120791");
assertEquals(driver.findElements(By.xpath("//li[#class='vak']")).size(), 1);
}
The part
driver.findElement(By.cssSelector("p.add")).click();
actually was successful, so after this call the colorbox should be loaded. However the
driver.findElement(By.xpath("//div[#id='profielpagina_add']/div[20]/a")).click();
causes an NoSuchElementException, while this element definitely is present in the colorbox when I check for myself (the call/xpath was even autogenerated by Selenium IDE). Does anyone have a clue what may cause the NoSuchElementException?
Probably you should wait when your popup appears. Try to use Implicit waits
WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Also instead of xpath you can use driver.FindElement(By.LinkText("Biologie")).click() if it the only link with text Biologie on your page