python find elements using selenium.webdriver - python

I want to find the following element:
<input type="text" value="" action-data="text=邮箱/会员帐号/手机号" action-type="text_copy" class="W_input " name="username" ...
And here is the html tags section, there are multiple input with the same name and class properties. So I want to find it using the normal_form div property.
This code does not work:
browser.find_element_by_css_selector('input[action-type="text_copy"]')
I think the field action-type is not a standard field.
What can I do?.
Thanks.
<div class="W_login_form" node-type="normal_form">
<div class="info_list" node-type="username_box">
<div class="inp username ">
<input type="text" value="" action-data="text=邮箱/会员帐号/手机号" action-type="text_copy" class="W_input " name="username" node-type="username" tabindex="1" maxlength="128" autocomplete="off">
</div>
</div>
I am trying, and this way I can find the element.
browser.find_element_by_xpath("//div[#class='W_login_form']/div/div/input")
It finds the div with class W_login_form first, and looks for div and div step in, and last gets the input.
Do you have any good idea about it?

Try this:
browser.find_element_by_xpath("//div[#class='info_list']//input")

Related

Selenium (python3) find an element by value and then select the button

I am trying to find a specific button when there are multiple buttons with the same name. So I have to find the element with the correct 'id' and then click the button nested within. I've been able to do this using the logical xpath but they change the path when new items are added to the page.
Right now, I'm just using:
driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div/div[4]/button').click();
<input type="hidden" name="data[brand_id]" class="brand-id" value="33" id="brand_id">
<div class="input number">
<label for="num-barrels=33">Number of barrels:</label>
<input name="data[num_barrels]" class="number-of-barrels" min="1" id="num-barrels=33" type="number" value="1">
</div>
<button class="add-brand-to-order btn btn-primary btn-block" type="submit">Add to Order</button>
</div>
You can use driver.find_element_by_id().click()
Here is the full documentation for this. Selenium

How to click the 2nd text box with smae class name in selenium using python

I have recently started using selenium with python and stuck with the below problem. It may be simple but I have tried a lot while searching through different answers but could not solve it.
I want to click the 2nd text box with class name 'param-text-input text-input numeric-value'
<div class="bet-widget-main-row-right">
<div class="bet-widget-main-content">
<div class="bet-params">
<div class="param-wrapper">
<span class="param-label">Label1</span>
<div class="param-input -desktop">
<div class="param with-error">
<span class="param-input-wrapper">
<span class="param-currency numeric-value">£</span>
<input type="text" class="param-text-input text-input numeric-value" value=".04" tabindex="0" size="3" maxlength="11">
</span>
</div>
<div class="param-input_ticks"></div>
</div>
</div>
<div class="param-wrapper">
<span class="param-label">Label2</span>
<div class="param-input -desktop">
<div class="param">
<span class="param-input-wrapper">
<input type="text" class="param-text-input text-input numeric-value" value="2.18" tabindex="0" size="4" maxlength="8">
</span>
</div>
<div class="param-input_ticks"></div>
</div>
</div>
</div>
</div>
<div class="bet-submit"><button class="confirm-bet-button -accented micro-button" type="submit" disabled="" tabindex="0"><span>Button1</span></button></div></div>
I have tried multiple solutions but none work:
self.driver.find_element_by_xpath("//[#class='param-text-inp‌​ut.text-input.numeri‌​c-value'][2]").clear‌​()
self.driver.find_element_by_css_selector("[input.param-text-‌​input.text-input.num‌​eric-value][2]").cle‌​ar()
self.driver.find_element_by_xpath("//input[#class='param-tex‌​t-input'][2]").clear‌​()
Any pointers/help is appreciated.
In the case that you would prefer to use a CSS selector, the following will choose that second input:
div.param-wrapper:nth-of-type(2) input.param-text-input.text-input.numeric-value
we're looking for the second "param-wrapper" because that is the thing which will correctly count the parameters.
It's pretty easy:
self.driver.find_elements_by_xpath('//input[#Class="param-text-‌​input.text-input.num‌​eric-value"]')[1].clear()
or
from selenium.webdriver.common.keys import Keys
self.driver.find_elements_by_xpath('//input[#Class="param-text-‌​input.text-input.num‌​eric-value"]')[1].send_keys(Keys.BACKSPACE)
You need to make an object list in the first place. So you should find all the placeholders with the same name. Then get said item, which is the second element(list are zero based, so 1 is second)
Cheers
It is possible in XPath to select the n-th object that corresponds to your XPath expression.
To achieve this you use the [n] at the end of of your expression.
What you forgot is to put () around your expression before adding the [n].
So your XPath should be (//input[#class='param-tex‌​t-input'])[2].
To click the 2nd text box with class name 'param-text-input text-input numeric-value' you can use the following code block :
self.driver.find_element_by_xpath("//div[#class='bet-params']//following-sibling::input[2]").clear()

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.

webdriver.support.select - Select doesn't interact with the element

Hello,
I have the following select:
<div class="col-xs-12 col-md-6">
<label class="ph_label pointer city">
<span class="l citylbl">City</span>
<select class="cities custom-dropdown" data-validation-error-msg="You must select a city" required="" name="city" disabled="disabled" data-validation="required">
<option value="">City</option>
<option value="Shanghai">Shanghai</option>
<option value="Beijing">Beijing</option>
<ul class="cities custom-dropdown" data-target-selector="select.cities" style="width: 293px;">
</label>
</div>
I'm trying:
mySelect = Select(self.driver.find_element_by_name("city"))
mySelect.select_by_value("Shanghai")
When I see the test run, selenium goes through this element as if everything was OK, but in fact the city was not selected.
Any ideia?
I think this is due to the select in the code snippet is marked as disabled?
I have tried using Watir-webdriver, and if the disabled="disabled" attribute is there, I get the behavior you describe. As soon as I remove it, the value I want is selected.

python selenium checkbox nth element if text

I am using python selenium and need to reach to the element, where the label is "thisis2ndID" and enable the checkbox?
<h3 class="activator"> … </h3>
<div class="accordion-panel">
<ul>
<li class="field">
<input id="thisis1stID" type="checkbox" name=""></input>
<label for="thisis1stID"> … </label>
</li>
<li class="field">
<input id="thisis2ndID" type="checkbox" name=""></input>
<label for="thisis2ndID"> … </label>
</li>
<li class="field">
<input id="thisis3rdID" type="checkbox" name=""></input>
<label for="thisis3rdID"> … </label>
</li
tried these but they dont seem to work...
browser.find_element_by_id('main-id').click()
browser.find_element_by_xpath("//div[#class='accordion-panel']")
browser.find_element_by_id(thisis2ndID').is_selected()
any ideas?
I have done this a couple of different ways, but the best method in your case is to iterate all the li tags and search for the id value you are looking for, try this
browser.find_element_by_xpath("//div[#class='accordion-panel']")
for i in browser.find_elements_by_tag_name("li"):
...
If there is provision to find elements using cssSelector in python, the follwing selector along with click() event will enable the checkbox....
"div.accordion-panel li.field+li.field>#thisis2ndID"
I've worked only on java, so i wont be able to provide exact code in python..
you can click here to know more about different kind of selectors
this worked...sorry for the confusion, the activator needed to be clicked to load the sub options...
browser.find_element_by_class_name('activator').click()
browser.find_element_by_xpath("//div[#class='accordion-panel']")
browser.find_element_by_id('thisis2ndID').click()
time.sleep(5)

Categories