unable to find multiple existing element with selenium - html

here is the html code of the Links i am trying to click on (i dont know why selenium cant locate the link. My guess is, that there are X amounts of the link. The only difference is the string in the brackets of onclick.) Underneath i'll show you 2 examples of the html code. I'd like to click on all of them (in this case on both)!
Button or Link:
<td class="text-right">
Ansehen</td>
Button or Link:
<td class="text-right">
Ansehen</td>
here are my Attempts to click on the button:
driver.find_element_by_link_text("doRetrievePnr('DUA75J')").click()
driver.find_element_by_xpath("//a[#onclick='doRetrievePnr('DUA75J')']").click()
Here are the Errors i get:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"doRetrievePnr('DUA75J')"}
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //a[#onclick='doRetrievePnr('DUA75J')'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//a[#onclick='doRetrievePnr('DUA75J')']' is not a valid XPath expression.
am I missing out on something?
EDIT
Heres a picture of the BUTTONS, the HTML CODE and my PYTHON CODE (lines 34 & 35)

As mentioned in the comments - getting all the items of a specific identifier can be done with find_elements_by_... (note find elements - it's plural)
That returns an array you can iterate through to interact with each element.
It looks like all your links have the same ID.
Something like this might work:
links = driver.find_elements_by_id('viewPnr')
for link in links:
link.click()
time.sleep(10) #replace this with what you want to do
Obviously if you're clicking links and the page changes this might not work after the first iteration. However, this is the concept on how you can get all objects and iterate through them.

Related

Parsing html with changing div id

I'm trying to parse the following HTML in order to get to the link that I marked below by using jsoup:
In order to do so, I did the following:
Document doc = Jsoup.parse( url );
Elements links = doc.select(".list-item-wrapper").select(".list-item")----> HERE IM STUCK
I would have continued by using:
doc.select(".list-item-wrapper").select(".list-item").select(#SEARCH_RESULT_RECORDID_dedupmrg914683993).select()....
But the problem is that _dedupmrg914683993 is changed between every page.
I also tried:
doc.select(".list-item-wrapper").select(".list-item").select(.list-item-primary-content result-item-primary-content layout-row).select()....
But I got 0 results.
How can I parse it so I could get eventually to the link inside <img class="main-img fan-img-1"...>?
Thank you
You can search for string match on any attribute, if your id always start with SEARCH_RESULT_RECORDID string you can look for it using the following syntax
doc.select(".list-item-wrapper").select(".list-item").select('[id^=SEARCH_RESULT_RECORDID]').select()....
I assuming that selectors are using jquery scheme

Using groovy script on Katalon to click on row of grid

With groovy script on Katalon, I want to click on first row of grid but it is not working and it shows error (error.png with Option1 mentioned below) in console.
I have tried by targeting different element's xpath of row but it seems click() event is not being recognized. Also, added wait and waitForPresentElement but did not work.
Following are three options I tried so far but none of them worked.
Groovy on Katalon:
Option 1:
WebUiCommonHelper.findWebElement(By.xpath("//div[#class='dgrid-row dgrid-row-even ui-state-default']/table/tr/td/span[#class='dijitIconFlag dijitFixedMatterIcon']")).click()
Option 2:
WebUiCommonHelper.findWebElement(By.xpath("//div[#class='dgrid-row dgrid-row-even ui-state-default']/table/tr/td[contains(#class,'dgrid-cell dgrid-cell-padding dgrid-column-0-')]").click()
Option3:
WebUI.click(findTestObject('Object Repository/Page_Law Practice Management Software CosmoLex/td_Client Funds-Trust_dgrid-cell dgrid-cell_865aa6'))
Source code:
Source code is in xPath.jpg.
Expected:
The row should be selected/clicked.
//table[#class="dgrid-row-table"]//tr//td
Use this as xpath,
and click by this manner.
Hope it will help you.
List<WebElement> objects = WebUiCommonHelper.findWebElements(findTestObject('***put xpath url here***'), 30)
WebUI.delay(1)
objects.get(0).click()

How do I check the Html.ValidationSummary for errors?

I am writing an mvc web app and I'm trying to check to see if there are any errors in the Html.ValidationSummary to determine if I need to display the div that handles the messages.
#if(Html.ValidationSummary() != null)
{
<div class="registration-val-summary">
<label class="registration-val-label">
Please correct the following issues with your registration:
</label>
#Html.ValidationSummary()
</div>
}
This is the error that I receive:
Unexpected "if" keyword after "#" character. Once inside code, you do
not need to prefix constructs like "if" with "#".
From what I read in this post: Unexpected "foreach" keyword after "#" character It is due to the "Html" that I have inside my code. I understand the error but I do not know how to get around it. I want to avoid displaying the div that wraps the ValidationSummary unless there are actually errors to display otherwise the message about correcting your error shows up and displaces my form.
How do I hide this if ValidationSummary has no messages to display?
You are recieving that error because you are rendering the partial view rasor code from Html.ValidationSummary() into your if statement. The compiler then sees any # symbols the validationSummary renders and fails.
A simple check to see if there are validation errors is to do #if(!ViewData.ModelState.IsValid)
#if(!ViewData.ModelState.IsValid)
{
<div class="registration-val-summary">
<label class="registration-val-label">
Please correct the following issues with your registration:
</label>
#Html.ValidationSummary()
</div>
}
Html.ValidationSummary() is displayed IF and ONLY IF there validation errors on the page.
What you are trying to do is already achieved by the Html.ValidationSummary() helper and it returns an HTML div element that contains an unordered list of all validation error messages from the model-state dictionary [Source]
Html.ValidationSummary() renders a partial view, but you can customize/extend the view by following this answer
In your case, you might want to put the following code inside the customized view that you will create
<div class="registration-val-summary">
<label class="registration-val-label">
Please correct the following issues with your registration:
</label>
</div>

"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.

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')]