Selenium click doesn't work but mouse does - html

Can anyone explain why/how in Selenium IDE, for an item I want to click on, the click doesn't work, however using mouseOver, mouseDown, mouseUp and mouseOut on the same Target does work. I have even tried to use multiple of those above, e.g.:
waitForElementPresent
mouseDown
mouseOver
mouseDown
click
mouseOut
This managed to work for me on something else, when using click didn't work. I also don't seem to be getting any errors and don't know what to do. Any help is appreciated.
UPDATE:
I clicked on the Find button in Selenium IDE and it highlighted the element, with all 6 of those Commands above, but it never actually clicks it when I run any of the commands or the case.

there are all sorts of implementations for click out there,
some use Javascript that listen to the mouse down event, some use the mouse up or click or mouse over the div or td they implemented as a clickable element and then the JS start working and you get the click action expected,
therefor there is no one correct event you should launch, rather test them all or execute them all,
i use the following commands and it works for most elements that doesn't work with a simple 'click' command:
<tr>
<td>focus</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td></td>
</tr>
<tr>
<td>mouseOver</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td></td>
</tr>
<tr>
<td>mouseMoveAt</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td></td>
</tr>
<tr>
<td>mouseDown</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td></td>
</tr>
<tr>
<td>mouseDownAt</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td></td>
</tr>
<tr>
<td>clickAt</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td>0,0</td>
</tr>
you can try more events until it works, if you are still experiencing problems then make sure your element path is correct (use the selenium firefox IDE and press "Find" button while the FireBug is open and it will colorize the element and it's Html code with yellow glowing color so you can make sure the element path is correct),
another trick is to right click the element and click "Wait for Text..." and copy the path from that command to the click and other commands i stated earlier
if element that you want to click is hidden (for instance a floating layer html) then you can change its attribute display:none to display:block in the firebug (temporary) and that way you can use the rightclick trick to extract the exact path

If you record a "Selenium Builder" script on a page that has an iFrame, I don't believe the recorder will record the switchTo event even though the mouse will have no problem accessing it. So, on replay, without the Selenium switchTo, a selenium even wont be able to click the element unless you manually add a switchTo frame call.

Try accessing the location_once_scrolled_into_view member variable (e.g., as in the following code).
ScrolledPosition = element.location_once_scrolled_into_view
element.click()
For some reason, that quite often allows the click to work even if your code doesn't actually use the position stored there explicitly.

I was able to get Selenium IDE to click the item, even though it doesn't seem to be the correct method in doing so. Below is the HTML for Selenium IDE.
<tr>
<td>mouseDown</td>
<td>css=div.dhtmlx_wins_btns_button.dhtmlx_button_close_default</td>
<td></td>
</tr>
<tr>
<td>clickAt</td>
<td>//div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_over_pressed']</td>
<td>0,0</td>
</tr>
Or you could use //div[#class='dhtmlx_wins_btns_button dhtmlx_button_close_default'] instead of the CSS locator.
I would still appreciate if someone has a better way of getting it to click than this as it still seems like the wrong way to go about it.

Also had same problem with clicking at element <td>. I used method selenium.clickAt(locator, coordString)
f.e.:
selenium.clickAt("element id - easily localized by firebug / firepath [firefox addon]","1.1");

Related

Angular 4 innerHTML removes directive from the element

I am trying to set innerHTML in a div but the directive used in the text is ignored. I am not sure if it is a invalid attribute(according to HTML) So below is my code:
<table class="table">
<tbody>
<tr>
<td>
<div [innerHTML]="issue.longText"></div>
</td>
</tr>
</tbody>
</table>
the value of issue.longText in component.ts is:
<ul><li>20/07/2018 - Hello!This is the test issue on click on it(<span [issueOpener]='{"recordId": 4, "page":1}'>link</span>)</li></ul>
And when I inspect the div I've got the following:
<ul><li>20/07/2018 - Hello!This is the test issue on click on it(<span>link</span>)</li></ul>
Directive issueOpener is removed from the span. So how can I prevent this directive? Any help would be greatly appreciated.
This is wanted behavior and here is why : when you write Typescript code, you aren't writing your final application. Your code needs to be compiled first.
To be compiled, your code goes through a process ran by the CLI : during this process, the compiler replaces every piece of Angular code with valid JS code.
This means for instance, that (click) event become onclick (This one's not entirely true, but that pictures the point I'm making).
When you use innerHtml, you write final code : it won't be compiled. It's not Angualr code, so it won't work like you hoped.
Ajay you can do one thing and that will surely work in your case.
Please refer following link:-
Angular 5 - How to call a function when HTML element is created with *ngFor
You can write logic on ngAfterViewInit() that should be responsible for adding directive at the HTML which is now loaded so that directive can be added dynamically.
Refer:-
How to dynamically add a directive?

"element does not exist in cache" error upon radio button select using mechanize::phantomjs

I'm trying to automate a webpage that includes a lot of javascript functions in Perl. So far I've been successful by using the headless mechanize::phantomjs toolkit, but I can't get past this, probably, trivial error. I see a page with a menu of radio buttons and when I try to select any one of those using
$mech->set_fields('booking_choice' => "$i");
where $i is my iterator and 'booking_choice' is the name of the button, I get the error listed below. This is how it looks on the page:
<tr bgcolor="white">
<td>
<input type="radio" name="booking_choice" value="1">
</td>
<td class="tableTxt">
Friday, January 16, 2015</td>
<td class="tableTxt">
08:45 a.m.</td>
<td class="tableTxt">
45 minutes</td>
</tr>
There are two other radio buttons with values 0 and 2, and that's what the iterator is for. However, within any one iteration upon selection of the button I get the following error:
Error while executing command: An element command failed because the referenced element is no longer attached to the DOM.: {"errorMessage":"Element does not exist in cache","request".....
I can locate the button on the page fine by using xpath and it always returns success:
if ($mech->xpath('//*[#name="booking_choice"]', one => 1)) {
say "Success";
}
What I don't understand is:
Why the element is no longer accessible even when I can see it listed on the html source page.
How can I keep the Element intact so that it is available for selection.
I'm very new to webscraping, web automation and I found some similar question to mine on this forum but they are either written for ruby or use some other webdriver than phantomjs. I don't know how to implement any of the given solutions using mechanize::phantomjs. I'll appreciate any help I can get. Thanks!
I've placed the source HTML file here: http://www.datafilehost.com/d/948c5371
mechanize requires you to select the form before you can set the form fields. This can be done with a variety of functions such as
$mech->form_name( $name [, %options] )
$mech->form_id( $id [, %options] )
$mech->form_number( $number [, %options] )
$mech->form_with_fields( [$options], #fields )
Taken from here.
If this doesn't work, you can always run some custom code in the page context to change the fields. For radio boxes this would look like this:
$mech->eval(<<'JS', $i);
document.querySelectorAll("[name='formName'] [name='radioButtonName']")[arguments[0]].checked = true;
JS
Although, this only changes the property of the radio button and in most cases this is enough. But sometimes there are event handlers on the DOM elements which will not be triggered in this case.
An alternative would be to actually use $mech->click( $name [,$x ,$y] ) to click on the radio button and trigger all necessary events.

for attribute in label behave differently in ie

For attribute perform differently in IE9 and IE10. When I clicking on label it is also checked-in checkbox in IE but in other browser it wont. fiddle
//JS
<script type="text/javascript">
function show(){
alert(0);
}
</script>
//HTML
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><input type="checkbox" id="test" /></td>
<td><label for="test">click</label></td>
</tr>
</table>
From the HTML5 specification:
The label element's exact default presentation and behavior, in
particular what its activation behavior might be, if anything, should
match the platform's label behavior. The activation behavior of a
label element for events targeted at interactive content descendants
of a label element, and any descendants of those interactive content
descendants, must be to do nothing.
For example, on platforms where clicking a checkbox label checks the
checkbox, clicking the label in the following snippet could trigger
the user agent to run synthetic click activation steps on the input
element, as if the element itself had been triggered by the user:
[... snipped code sample ...]
On other platforms, the behavior might be just to focus the control, or do
nothing.
The specs seem to leave a lot of freedom even though, basically, they say that on clicking on a label, if a browser really has to do something, then it should do what normally happens on the current platform.
For example, if I recall correctly, in Windows when you click on a label (in a Wdinows form) the checkbox receives the click as well, so this is what should happen in a browser running in Windows.
Again, since there is a lot of freedom, pretty much every browser does what it likes.
If the question, which is not very clear, is how to avoid this behaviour, you may have to work around it some way using jquery or javascript. This seems to work, after a first quick test in IE10:
<label for="test">
jitender
</label>
made a few changes, heres the fiddle
removed the link from label though
<label for="test" onclick="javascript:show()"> jitender</label>

Selenium Test Case/Suite

My Selenium Test Case and Test Suite is not working properly, for some reason when I play each test case separately, it works, however, when I play the whole thing together, it says: 'Element ... not found' and also, if I add both test cases to a new test case and play this new test case, it doesn't work. I am assuming my first test case is fine as that always works, but it gets the element not found error at this point, and I have tried other ways as well:
<tr>
<td>click</td>
<td>//div[#class='btn_sel_text' and contains(., 'Volume Entry')]</td>
<td></td>
</tr>
I also tried replacing the '//div' with:
css=.btn_sel_text:contains('Volume Entry')
however that didn't work either. Can anyone please explain why it doesn't work when I run the whole thing together, however, if I simply run the //div or the 'css' on it's own it does work.
put a pause before click.
command:pause
Target:3000
Try it,Thank you.
Find Xpath of that element using firepath. with Xpath of element you can click on it. do this & let me know your problem is solved or not
<tr>
<td>pause</td>
<td></td>
<td>1500</td>
</tr>
<tr>
<td>click</td>
<td>Xpath of the element </td>
<td></td>
</tr>
In trying to debug this problem for myself, I found that if I had two test cases that worked individually and then when I ran them together in a test suite a link/button could not be found in the second test case, the solution was actually to modify the FIRST test case.
If you have more than two test cases, try changing the order around so that there is some other test case immediately following the original first test case. When I tried that, my error moved from the original second test case to the new second test case.
I don't know exactly why, but it is like Selenium is unable to catch up with the effects of the first test on the browser or something. If add, however, a 'pause 5000' followed by a ClickAndWait (to an inocuous link that keeps me on the same page, perhaps) to the end of the
first test case, then when the first and second test case are run together in the test suite, the second test case now works.

Trying to click on a hyperlink for a row with specific value in Selenium IDE

I'm new to Selenium, There are 2 things I need to do
1. To take a note of the file name on the html but its part of a string so dont know how to concatenate it in Selenium IDE
2. In a table, after searching for the relevant file, I would like to click on a link in one of the other columns for that row. So For the file EMED.pdf, I would like to click the Requested link
HTML snippet:
<td>
<a class="OSfileLink" target="selenium_blank7934" href="/Practice/DownloadFile/8057329d-21df-4a44-baee-2068fc01b7dd" rel="EMED.pdf" style="background-color: transparent;">EMED.pdf</a>
</td>
<td></td>
<td> 27/02/2013 16:19:16 </td>
<td>
<a class="eApproval " data-toggle="modal" data-fileid="8057329d-21df-4a44-baee-2068fc01b7dd" href="#" rel="Requested (28/02/2013)" style="background-color: transparent;">Requested (28/02/2013)</a>
</td>
So far I have :
Command: click
Target: //a[contains(text(),'Requested ')]/following-sibling::a[contains(text(),'EMED.pdf')]
value :
This brings up an error:
[error] Element //a[contains(text(),'Requested ')]/following-sibling::a[contains(text(),'EMED.pdf')] not found
I can "Find" the individual xpath's seperately, but they dont work together. Any help will be appreciated.
Edit
Still no joy with the concatenation, but can work round it with the click being identified.
Any help with concatenation will be much apprciated
* EDIT
Concatenation: For: href="/Practice/DownloadFile/8057329d-21df-4a44-baee-2068fc01b7dd"
I'd like the guid: 8057329d-21df-4a44-baee-2068fc01b7dd from the whole link.
Thanks
Looks like you have your order swapped, try replacing following-sibling with preceding-sibling. Also note you want the <a> nodes, but the following-sibling function is checking the next node after <a>, which is <td>, so you'll need to start with the <td> nodes and check the content of each <a> node within.
Or the other way around, you could change which node you're starting your search with. If you want the "EMED.pdf" but only if the next node is "Requested", try something like this:
//td[contains(a/text(),'EMED.pdf')]/following-sibling::td[contains(a/text(),'Requested')]/a
based on the html you put up, I think this works, I tested it with firefox xpath tester
//.[td[a[contains(text(),'EMED.pdf')]]]//a[contains(text(),'Requested')]