Unable to click on date from calendar popup - selenium

I wanted to click the date from calendar popup which is inside iframe. I have written below selenium code which throwing unable to locate the element error. Please help me to write correct xpath or selenium code. I am new to Selenium
selenium code:
WebElement iframe =driver.findElement(By.id("NewsSearchDateToInput_selector_iframe"));
driver.switchTo().frame(iframe);
//clicking on date 3
driver.findElement(By.xpath("//div[3][#class='daysNumbersStyles']")).click();
Error: unable to locate the element "//div[3][#class='daysNumbersStyles']"
Html tags:
<iframe id="NewsSearchDateToInput_selector_iframe"
class="dateTimeSelectorContainerStyle altFlexibleContainer"
src="javascript:false;"
style="left: 1216px; top: 245px; width: 249px; height: 207px;
display: block;"/>
<div class="dateTimeSelectorContainerStyle altFlexibleContainer"
style="top: 245px; left: 1216px; display: block;">
<div class="top">
<div class="content">
<div class="dateSelectorHeader">
<div class="dateSelectorBody">
<div class="yearMonthSelectorStyle">
<div id="NewsSearchDateToInput_selector_monthSelector" class="monthSelectorListStyle">
<div class="daysStyle">
<div>
<div class="daysNumbersDivStyle">
<div class="daysNumbersStyles">1</div>
<div class="daysNumbersStyles">2</div>
<div class="daysNumbersStyles">3</div>

does it help ? //div[#class='daysNumbersStyles'][3]

Try this after getting in to frame:
driver.findElement(By.xpath("//div[text()='3']").click();
Edit:
For To selector:
driver.findElement(By.xpath("//*[#id='NewsSearchDateToInput_selector_monthSelector']//div[text()='3']").click();
For From selector:
driver.findElement(By.xpath("//*[#id='NewsSearchDateFromInput_selector_monthSelector']//div[text()='3']").click();

Related

Selenium webdriver can't find xpath

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()

SeleniumIDE doesn't find my Target

Allready tried "intro to css locators in selenium" and "css selectors for selenium"but they did not bring a solution. When using Selenium IDE 2.91 to record a webpage test, playing back the scenario ends in errors. IMHO the problem is a push button with aria-pressed that changes when hovering over it, pressed or unpres it. When doing those things the div class is changing from:
<div class="gwt-PushButton A gwt-PushButton-up" tabindex="0" role="button" style="" aria-pressed="false">
To
<div....gwt-PB-up-hovering tabindex="0" role="button" style="" aria-pressed="false">
And to
<div class="gwt-PushButton A gwt-PushButton-down-hovering" tabindex="0" role="button" style="" aria-pressed="true">
Rest of html code within the div is:
<input tabindex="-1" role="presentation" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute;" type="text">
<div class="html-face">Controle</div>
</div>
Record of selenium is 2 records:
1 click //div[5]/div[2]
2 click //div[5]/div[2]/div
The info selenium gives = [info] Executing: |click | //div[5]/div[2] |
| But because the button isnt really clickt on the next step goes in
error.
Any suggestions how to be able to make selenium click the button?
Already tried click using css=div.gwt-PushButton.GBKHVCADO2

Dynamic Element handling

In our application there is a task view page. For different tasks, xpath for the same button found change every time as below
WIN_1_1000000014
WIN_2_1000000014
WIN_3_1000000014
WIN_4_1000000014
WIN_5_1000000014
I am providing html codes for two of such scenarios below . I am using following xpaths to find that button but not working for me.
//div[#ardbn='Assignee Organization']//a[#class = 'btn btn3d menu']//img
//img[#alt='Menu for Organization']
Help me guys
<div id="WIN_1_1000000014" class="df arfid1000000014 ardbnAssigneeOrganization Char" arid="1000000014" artype="Char" ardbn="Assignee Organization" arlbox="0,4,78,17" ardcf="1" style="z-index:998;top:84px; left:5px; width:263px; height:21px;" arwindowid="1">
<label id="label1000000014" class="label f6" for="x-arid_WIN_1_1000000014" style="top: 4px; left: 0px; width: 78px; height: 17px;">Organization</label>
<textarea id="arid_WIN_1_1000000014" class="text sr " cols="20" maxlen="60" style="top:0px; left:83px; width:154px; height:21px;" armenu="CTM:SGP:SupportOrganization3-Q" mstyle="2" arautoc="1" arautocmb="1" arautocak="0" arautoctt="400" rows="1" title="Service_Desk_First_Tier"
wrap="off"></textarea>
<a class="btn btn3d menu" href="javascript:" style="top:0px; left:242px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="Menu for Organization" title="" style="background-color: transparent;">
</a>
<div id="WIN_3_1000000014" class="df arfid1000000014 ardbnAssigneeOrganization Char" arid="1000000014" artype="Char" ardbn="Assignee Organization" arlbox="0,4,78,17" ardcf="1" style="z-index:998;top:84px; left:5px; width:263px; height:21px;" arwindowid="3">
<label id="label1000000014" class="label f6" for="x-arid_WIN_3_1000000014" style="top: 4px; left: 0px; width: 78px; height: 17px;">Organization</label>
<textarea id="arid_WIN_3_1000000014" class="text sr " cols="20" maxlen="60" style="top:0px; left:83px; width:154px; height:21px;" armenu="CTM:SGP:SupportOrganization3-Q" mstyle="2" arautoc="1" arautocmb="1" arautocak="0" arautoctt="400" rows="1" title="Service_Desk_Resolver"
wrap="off"></textarea>
<a class="btn btn3d menu" href="javascript:" style="top:0px; left:242px; width:21px; height:21px;">
<img class="btnimg" src="../../../../resources/images/mt_sprites.gif" alt="Menu for Organization" title="" style="background-color: transparent;">
</a>
you can write the xpath for the latest task as below:
(//div[contains(#id,'WIN_')])[last()]//img[#alt='Menu for Organization']
Use below code for click or any other operations:
List<WebElement> elements = driver.findElements(By.xpath("//img[#alt='Menu for Organization']"));
for(int i=1;i<=elements.size();++i){
elements.get(i).click();
}
xpath working for 1st 3 elements is
(//div[#ardbn='Assignee Organization']//a[#class = 'btn btn3d menu']//img)[last()]
xpath working for rest elements is
(//div[#ardbn='Assignee Organization']//a[#class = 'btn btn3d menu']//img)[position()<3]

Unable to get the values from <p> tag

I should get the values between the p tag.
Following is the code from which i need to get the values
<div id="ved-list-totals">
<div id="ved-sidebar-totals" class="clearfix margin-top-10" style="height: 100px;">
<div id="ved-sidebar-totals" class="clearfix margin-top-10" style="height: 100px; background-color: transparent;">
<div class="pull-right margin-left-20 margin-right-10 align-right">
<p class="no-margin">0</p>
<p class="no-margin" style="background-color: transparent;">5.97</p>
<p class="no-margin">0.00</p>
<p class="no-margin">4.95</p>
<p class="no-margin bold dark-text">10.92</p>
</div>
<div class="pull-right margin-left-20 align-right">
<p class="no-margin">Estimated Points</p>
<p class="no-margin">Subtotal</p>
<p class="no-margin">Tax</p>
<p class="no-margin">Service Fee</p>
<p class="no-margin bold dark-text">Estimated Total</p>
</div>
I have tried the following approaches:
String tot = driver.findElement(By.xpath("html/body/div[4]/table/tbody/tr/td[2]/div/div[1]/div[2]/div/div/div/div[2]/div/div/div[1]/p[2]")).getText();
String axd = driver.findElement(By.xpath("html/body/div[4]/table/tbody/tr/td[2]/div/div[1]/div[2]/div/div/div/div[2]/div/div/div[1]/p[3]")).getText();
String tot = driver.findElement(By.xpath("//div[#id='ecart-sidebar-totals']/div/p[4]")).getText();
String axd = driver.findElement(By.xpath("//div[#id='ecart-sidebar-totals']/div/p[3]")).getText();
I was getting the error as :
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//div[#id='ecart-sidebar-totals']/div/p[4]"}
There are two elements with the same id (<div id="ved-sidebar-totals">) should this id be blamed for this error, or is there any thing which am missing.
Use the xpath //div[#id='ved-list-totals']//p[1]. This should return the first p tag value with getText() method.
Wild guess..
//div[#id='ecart-sidebar-totals']/div[#id='ved-list-totals']/p[1]
Try using Css selector
driver.findElement(By.cssSelector("div[class='pull-right margin-left-20 margin-right-10 align-right'] p:nth-child(4)"));

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