Selenium Python - Select input-element berore span-element - selenium

Web page have element with structure like:
<label>
<input type="checkbox" name="storage_locations" value="3" style="vertical-align: middle;">
<span style="padding-left: 6px;">example.domain.com: [Text] Text_1 (Sometext)</span>
</label>
<label>
<input type="checkbox" name="storage_locations" value="3" style="vertical-align: middle;">
<span style="padding-left: 6px;">example.domain.com: [Text] Text_2 (Sometext)</span>
</label>
<label>
<input type="checkbox" name="storage_locations" value="3" style="vertical-align: middle;">
<span style="padding-left: 6px;">example.domain.com: [Text] Text_3 (Sometext)</span>
</label>
How i can select input-element located in label with span-element which contains 'Text 2' (for example)?
I know that i could find span-element using this code:
example = driver.find_element(By.XPATH,'//*/label/span[contains(text(),"Text_2")')
But i'm new in selenium-python and haven't ideas what to do next

There are always multiple ways to do things.
You can try with css selector:
driver.find_element(By.CSS_SELECTOR, "body > label:nth-child(2) > input[type=checkbox]")
Or with name:
driver.find_element(By.NAME, "storage_locations")
You can always do it with xpath too. Hope this helps.

I solved my question with help of two array.
text_list = 'text1,text2,text3,ect'
text_array = text_list.split(',')
input_checkbox_array = driver.find_elements(By.XPATH,'//*[xpath_to_find_checboxes')
span_text_array = driver.find_elements(By.XPATH,'//*[xpath_to_find_inputs')
for datastore in datastores_array:
i = 0
while datastore not in span_text_array[i].text:
i += 1
actions.move_to_element(input_checkbox_array[i])
actions.click(input_checkbox_array[i])
actions.perform()

Related

It is possible to handle condition from CSS style in selenium?

To check which input is enable, in our DOM structure(Knockout JS) there is no specific property, which says the button is enable..
Its handled by one of the CSS property, of ::before class :
CSS: {content: "\f013";}
HTML:
<div data-bind="foreach: XXXXXX ">
<div class=".col-xs-3 col-sm-3 col-md-3 -setting-radio" data-bind="attr: " >
<label class="radio-position radio-inline " data-view="widgets/input/radio/view" data-active-view="true" style="">
<input class="-widgets-input-radio-check" id="radio" type="radio" data-bind="Enable: enable, value: value, checked: checked" value="1" name="FS">
<span class="-radio-label"></span>
<span class="-widgets-input-radio-text" data-bind="html: data">Global</span>
</label>
</div>
<div class=".col-xs-3 col-sm-3 col-md-3 -setting-radio" data-bind="attr: " >
<label class="radio-position radio-inline" data-view="widgets/input/radio/view" data-active-view="true" style="">
<input class="-widgets-input-radio-check" id="radio" type="radio" data-bind="Enable: enable, value: value, checked: checked" value="2" name="FS">
<span class="-radio-label"> </span>
<span class="-widgets-input-radio-text" data-bind="html: data">Capital</span>
</label>
</div>
</div>
How can we achieve it ?
There is a special method for this in selenium, named value_of_css_property The name differs on the language you use, but the idea is the same.
As I understand from your question you can use it like as in the below example:
assert driver.find_element_by_css_selector('css_locator').value_of_css_property("content") == "\f013"
HTML is not enough, you need to check which CSS properties are changed as well.
You can use it to for waits and asserts.
I use computed tab for this:

How to access nested input attribute using Jsoup

Please could You help my with selecting int value from input attr"value"? In this case "12".
<td class="fit-content">
<div class="add-cart" data-original-title="Ilość w op. zbiorczym: 12 szt.">
<div class="input-group">
<input class="form-control form-control-sm" type="text" value="12">
</div>
</div>
Try
String value = doc.selectFirst("div.add-cart div.input-group input").attr("value");

Unable to find element using protactor which have same classes

I have one check box and two links with same classes & same Div.During the automation testing using protractor, i want to click on check box but it click on Links.
i am writing this code but its not working, please provide a solution.
Code:
element(by.id('Remember')).click();
[1
please find HTMl code:-
<div class="input-field">
<div class="pas_rembr">
<input name="remember" id="Remember" class="css-checkbox ng-dirty ng-valid-parse ng-touched ng-not-empty ng-valid ng-valid-required" ng-model="rememberMe" type="checkbox" ng-required="true" required="required">
<!-- <label for="Remember" class="css-label">I agree with the <a class="text_link" target="_blank" ng-href="{{baseUrl}}terms-conditions">Terms & Conditions</a>.</label> -->
<label for="Remember" class="css-label">I have read and I agree with <a class="text_link" target="_blank" ng-href="/lmd/terms-conditions" href="/lmd/terms-conditions">Terms and Conditions</a> and the <a class="text_link" target="_blank" ng-href="/lmd/privacy" href="/lmd/privacy">Privacy Policy</a> of this site.</label>
</div>
<span class="errorForm ng-hide" ng-show="(memberForm.remember.$dirty || submitted) && ((memberForm.remember.$error.required))">
<span class="errorForm ng-scope ng-hide" ng-show="memberForm.remember.$error.required" translate="TERAMS_CONDITION_IS_REQUIRED">Terms and condition is required</span>
</span>
</div>
Try this:-
var el = element(by.css('label[for="Remember"]'));
browser.actions().mouseMove(el, {x: 20, y: 3}).click().perform();
For your tests, the CSS selector would be
element = $$('label.css-label > a:nth-child(2)');
This should be able to click on the second a child of the label element.

Element should have been "select" but was "span" in selenium

Toggle button is present in an web application similar to
http://www.dhtmlgoodies.com/index.html?whichScript=on-off-switch
Search for "option two" in the above link.
Html code of the button is like this when turned Off
<div class="field-block button-height">
<label style="width:175px" class="label" for="input-2"><b>Case Association</b><sup>*</sup>
<span id="reqCaseAssociationTD" style="display: none;">
required</span>
</label>
<p style="margin:0px" class="field switch">
<label style="margin:0 0 4px" class="cb-enable"><span>On</span></label>
<label style="margin:0 0 4px" class="cb-disable selected"><span>Off</span></label>
<input type="checkbox" class="checkbox" id="caseAssociation">
</p>
<span style="margin-top:8px;margin-left:2px" data-tooltip="Simulataneous post from the same user (Twitter and Facebook) would get associated with the previous open case of the user." class="helpText showToolTip"></span>
</div>
Html code of the button is like this when turned ON
<div class="field-block button-height">
<label style="width:175px" class="label" for="input-2"><b>Case Association</b><sup>*</sup>
<span id="reqCaseAssociationTD" style="display: none;">
required</span>
</label>
<p style="margin:0px" class="field switch">
<label style="margin:0 0 4px" class="cb-enable selected"><span>On</span></label>
<label style="margin:0 0 4px" class="cb-disable"><span>Off</span></label>
<input type="checkbox" class="checkbox" id="caseAssociation" checked="checked">
</p>
<span style="margin-top:8px;margin-left:2px" data-tooltip="Simulataneous post from the same user (Twitter and Facebook) would get associated with the previous open case of the user." class="helpText showToolTip"></span>
</div>
Used the following identifiers
//Case Association Turned On
#FindAll(#FindBy(how = How.CSS, using = "a.cb-enable.selected"))
public List<WebElement> caseAssociationIsON;
//Case Association Turned OFF
#FindAll(#FindBy(how = How.CSS, using = "a.cb-disable.selected"))
public List<WebElement> caseAssociationIsOFF;
While running using the selenium getting the error
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element
should have been "select" but was "span"
How do fix this. If more details are needed, please suggest.
I don't see any element that would match your selector, you should use label instead of a, like:
label.cb-enable.selected
and
label.cb-disable.selected
or use a selector based on input. a means link and you don't have any.
In case you still have the error what happens is that you must add an specific route:
In my case:
enter image description here
This is the resultant code:
enter image description here

Selenium: Select RadioButton in Label by Value (or Label Text)

I'm new to Selenium and want to select a RadioButton inside a group of labels, see Screenshot. The problem is, that all <input> elements have the same name! So I have to select them by the value (or by the label text?)...
I'm using the Java API for Selenium.
** As HTML **
<table width="100%" border="1">
...
<label>
<input type="radio" name="AktarmaSekli" value="SP" checked class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Sipariş Planı</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="DF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">DAG Fatura Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="ATR" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">ATR Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AITM" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">AİTM Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="COC" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">CoC Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="Bakim" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Bakım Faturaları</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Araç Bilgileri</label></td></tr>
</table>
Good to know you have solution.. below one may also helps..
If you are trying to click on specific button, if it has value say 'ATR' you can build xpath (or css selector) simply //input[#value='ATR'] or //input[contains(text(),'ATR Bilgileri')]
off-course other ways also there to find required element..
Thanks,
Murali
I got it to work with the following code!!!
private void selectRadioButtonByValue(WebDriver driver, String radioGroupName, String valueToFind) {
List<WebElement> radioGroup = driver.findElements(By.name(radioGroupName));
for (int i = 0; i < radioGroup.size(); i++) {
if(radioGroup.get(i).getAttribute("value").equals(valueToFind)) {
radioGroup.get(i).click();
return;
}
}
throw new org.openqa.selenium.NotFoundException("Element wasn't found by value!");
}
In Page Object Pattern it will be:
#FindBy(xpath = "//*[contains(text(),'ATR Bilgileri')]")
WebElement radioATR;