Robot Framework, Python, Selenium "Page Should Contain" do not work - python

Here is my code :
${verifications} Create List
Set Test Variable ${verifications}
Input text ${societes_input_nom} ${nom_etablissement} ${TEST_NAME}
Append To List ${verifications} ${nom_etablissement} ${TEST_NAME}
Input text ${societes_input_contact_email_casino} ${email}
Append To List ${verifications} ${email}
Input text ${societes_input_contact_adresse} ${adresse}
Append To List ${verifications} ${adresse}
Input text ${societes_input_site_web} ${url_siteweb}
Append To List ${verifications} ${url_siteweb}
FOR ${item} IN #{verifications}
Page Should Contain ${item}
END
Here the HTML for the input :
<div class="form-group">
<label for="title"><h4>Nom de l'établissement
</h4></label>
<br>
<div class="input-group">
<div class="input-group-addon" >
<i class="fa fa-building">
</i>
</div>
<input type="text" style="display: none;" id="initialvalue_prop_1" name="initialvalue_prop_1" maxlength="1000" value="TNR Activite Casino"
/>
<input type="text" id="prop_1" name="prop_1" maxlength="1000" value="TNR Activite Casino"
/>
But, the "page should Contain" do not find my ${nom_etablissement} ${TEST_NAME}
But when i'm looking the page, thoses words are in the page (and the DOM)
The error :
Page should have contained text 'TNR Activite Casino' but did not.
I don't know how to fix that, I tried many thing but nothing work...
Can you help me pls ?

Page Should Contain check for actual text and not the values of the attribute. To get value of attribute you should use -
${Value}= Get Element Attribute xpath://input[#id='initialvalue_prop_1'] value
If you want to assert value inside the textfield then use the keyword -
Textfield Value Should Be ${textfield_locator} ${value_to_be_asserted}
For more details Get Element Attribute

Related

How to access multiple text boxes in selenium

i am trying fill the text boxes. Each time you get into the page the the number of text boxes will be different and the name of them will be different.
<div class="fb-single-line-text">
<div class="display-flex" data-test-single-line-text-input-wrap="true">
<!---->
<input name="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)" id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)" class="ember-text-field ember-view fb-single-line-text__input" aria-required="true" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257397,numeric)-error-message" data-test-single-line-text-input="" type="text">
</div>
</div>
the above lines are the html code of first text box
<div class="fb-single-line-text">
<div class="display-flex" data-test-single-line-text-input-wrap="true">
<!---->
<input name="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257389,numeric)" id="urn:li:fs_easyApplyFormElement:(urn:li:fs_normalized_jobPosting:2620509178,30257389,numeric)" class="ember-text-field ember-view fb-single-line-text__input" aria-required="true" aria-describedby="urn:li:fs_easyApplyFormElement:(urn:li:fs_norm
alized_jobPosting:2620509178,30257389,numeric)-error-message" data-test-single-line-text-input="" type="text">
</div>
</div>
So i see all of them class name in comman so i tried to get all the eemphasized textlemnts inside the class . but i could not find a possible way for it. can someone help me with that
There are multiple ways to deal with this situation.
use find_elements to find out how many input boxes are present in UI, also you could send different message to those input box.
total_no_of_input_with_div_single_line_text = len(driver.find_elements(By.CSS_SELECTOR, "div.fb-single-line-text input"))
print(total_no_of_input_with_div_single_line_text)
Now you can send_keys to individual elements like this :-
total_no_of_input_with_div_single_line_text[0].send_keys('some string')
you can always change the index above from 0 to 1, 2 etc.
second in a loop like this :
for ele in driver.find_elements(By.CSS_SELECTOR, "div.fb-single-line-text input"):
ele.send_keys('some value here')
other way would be to use xpath indexing to locate the element.
(//div[#class='fb-single-line-text']/descendant::input)[1]
should locate the first web element input in the page, also you have the access to change that to [2], [3] and so on..

XPath for attribute containing spaces

I'm trying to select a checkbox attribute in xpath which is named "checked type".
I've tried //[#checked type], //[#checked-type], //[#checked_type], and //[#checked\stype] but none of them appear to be selecting any elements, while //*[#type] is working fine.
Is it possible to select an attribute with spaces?
checked and type are 2 attributes but not one, that's the reason why you are not able select the element using //[#checked type], //[#checked-type], //[#checked_type], and //[#checked\stype] locators.
You can define the checkbox status using checked attribute as shown in the below sample html.
<!DOCTYPE html>
<html>
<body>
<div>
<label> Checkbox Checked - Using checked attribute value</label>
<input checked='checked' type='checkbox'></input>
</div>
<div>
<label> Checked - simple approach</label>
<input checked type='checkbox' ></input>
</div>
<div>
<label> No Checked attribut</label>
<input type='checkbox'></input>
</div>
<div>
<label> Radio Checked - Using checked attribute value</label>
<input checked='checked' type='radio'></input>
</div>
<div>
<label> Radio Not Checked - No checked attribute value</label>
<input type='radio'></input>
</div>
</body>
</html>
If you want to set the checkbox/radio 'selected' by default then you can use either checked without attribute value or checked='checked'.
Refer to checked attribute in this page for more information.
So now, let's see how you can select the element.
Checkbox
//input[#checked][#type='checkbox']
//input[#checked='checked'][#type='checkbox']
Radio:
//input[#checked][#type='radio']
//input[#checked='checked'][#type='radio']

using nth-type or nth-child to select n element

Q: What XPath or CSS selector I can use to select 2nd <div class="checkbox">?
I have tried to use:
XPath - //div[#class="checkbox"][2]
CSS - div.checkbox:nth-child(2)
However none of them worked on chrome developer tool.
I can use $x('//div[#class="checkbox"]') to see all three checkboxes
I can use $x('//div[#class="checkbox"]')[0] to specify the 1st div.checkbox
I can use $x('//div[#class="checkbox"]')[1] to specify the 2nd div.checkbox
Here's an example of my HTML Structure
<div class="fs">
<div class="f">
<div class="checkbox">
<input type="radio" value="A">
<label for="A">A</label>
</div>
</div>
<div class="f">
<div class="checkbox">
<input type="radio" value="B">
<label for="B">B</label>
</div>
</div>
<div class="f">
<div class="checkbox">
<input type="radio" value="C">
<label for="C">C</label>
</div>
</div>
</div>
Rather than trying to find the second element by index, another possibility would be to get it by the value on the INPUT or the text in the LABEL that is contained in that DIV. A couple XPaths would be
//div[#class='checkbox'][./input[#value='B']]
//div[#class='checkbox'][./label[.='B']]
You need 2nd element from the results. Which can be done by using below
(//div[#class="checkbox"])[2]
I think CSS doesn't allow such a thing to select from a result
Since JeffC and Tarun Lalwani already suggested XPath way of doing it, I'd like to suggest a different approach.
In CSS, one can use :nth-child selector to choose 2nd <div class="f"> and grab the nested div from there. (> can be omitted)
div.f:nth-child(2) > div.checkbox
Similarly, the following works in XPath:
//div[#class='f'][2]/div[#class='checkbox']
One can choose an element based on the attribute value with CSS selector using Attribute selectors, but one cannot select the parent, unfortunately.

How to select an element from a dropdown(checkbox list)

I need to be able to select one item from a dropdown list (the items in the list are checkboxes) but I cannot seem to get the code correct. The web page is an internal site so I cannot provide the actual link, however here are they Elements:
To activate the dropdown list:
<input name="m_sqlRsWebPart$ctl00$ctl19$ctl06$ctl09$txtValue" type="text" size="28" readonly="readonly" id="m_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09_txtValue" title="Support Group report parameter" class="null">
The item from the list I need is:
<input id="m_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09_divDropDown_ctl184" type="checkbox" name="m_sqlRsWebPart$ctl00$ctl19$ctl06$ctl09$divDropDown$ctl184" onclick="$get('m_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09').control.OnValidValueClick(this, 'm_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09_divDropDown_ctl00');">
What I have tried so far is(snippet):
driver.find_element_by_name("m_sqlRsWebPart$ctl00$ctl19$ctl06$ctl09$txtValue").click()
driver.find_element_by_id("m_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09_divDropDown_ctl184").click()
Does not do anything.
Full HTML for the list box:
<div onactivate="event.cancelBubble=true;" style="white-space:nowrap;">
<input name="m_sqlRsWebPart$ctl00$ctl19$ctl06$ctl09$txtValue" type="text" size="28" readonly="readonly" id="m_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09_txtValue" title="Support Group report parameter" class="null"><input src="/sites/pa/reports/Reserved.ReportViewerWebPart.axd?OpType=Resource&Version=11.0.5058.0&Name=Microsoft.Reporting.WebForms.Icons.MultiValueSelect.gif" name="m_sqlRsWebPart$ctl00$ctl19$ctl06$ctl09$ddDropDownButton" type="image" id="m_sqlRsWebPart_ctl00_ctl19_ctl06_ctl09_ddDropDownButton" alt="Select a value" title="Select a value" style="vertical-align:top;cursor:pointer;">
</div>

Get text from a label using Selenium (Python)

I'm trying to read the text from a label that changes according to certain conditions (eg. if you enter a Username already in use, the label will display "Username already in use").
I've been trying to read in the text from the label that gets displayed, but nothing I've tried has worked so far.
The HTML looks like this:
<div class="margin-top-10">
<span ng-show="sentValidation">
<span id="test1" ng-show="userNameAvailable" class="txt-color-green">
<i class="fa fa-check fa-lg"></i>Username available
</span>
<span id="test2" ng-show="!userNameAvailable && userNameAvailable != null" class="txt-color-red">
<i class="fa fa-exclamation-circle"></i>Username unavailable
</span>
</span>
<div class="txt-color-red" ng-show="form.cUsername.$dirty && form.cUsername.$invalid">
<p id="test3" ng-show="form.cUsername.$error.required">
<i class="fa fa-exclamation-circle"></i>Username is required
</p>
<p id="test4" class="txt-color-red" ng-show="form.cUsername.$error.maxlength">
<i class="fa fa-exclamation-circle"></i>Maxmium length is 50 characters
</p>
<p id="test5" class="txt-color-red" ng-show="form.cUsername.$error.minlength">
<i class="fa fa-exclamation-circle"></i>Minimum length is 4 characters
</p>
</div>
</div>
Does anyone know how I can read what gets displayed on the screen?
content = driver.find_element_by_class_name('fa-exclamation-circle')
print (content.text)
I didn't manage to find a way to read the text, but I found another solution. Instead of checking what text is displayed on the screen, I check to see which of the labels is being displayed (I'm able to do this because each of the labels is given a unique ID.
So the code will look something like:
Name=driver.find_element_by_id('test2')
if Name.is_displayed():
print ("Element found")
else:
print ("Element not found")
content = driver.find_element_by_class_name('fa-exclamation-circle')
content.is_displayed()
You can use the find_element_by_class_name built in method.
You need to pass the class name as a parameter.

Categories