I have a complicated div section like below , I am trying to insert name after check label name. Is it possible in python selenium ?
<div class="section_1">
<label>
<div>Name</div>
</label>
<div>
<input type="text" name="b4bf265013b0ac0">
</div>
</div>
I am trying something like that, but not getting any solution.
label = driver.find_element_by_xpath('//form//label')
if lable == 'Name'
then select this lable field
You can use any the of following xpath
to achieve this.
Xpath 1:
input=driver.find_element_by_xpath("//label[./div[text()='Name']]/following-sibling::div[1]/input")
Xpath 2:
input=driver.find_element_by_xpath("//label[contains(.,'Name')]/following-sibling::div[1]/input")
Xpath 3:
input=driver.find_element_by_xpath("//label[contains(.,'Name')]/following::input[1]")
Xpath 4:
input=driver.find_element_by_xpath("//label[./div[text()='Name']]/following::input[1]")
You can parameterized the field as well.
lableName="Name"
input=driver.find_element_by_xpath("//label[./div[text()='{}']]/following-sibling::div[1]/input".format(lableName))
To select an element by the text it contains, you can simply use find_element_by_link_text
like this:
div = driver.find_element_by_class_name('section_1')
label_div_text = div.find_element_by_link_text("NAME").text
If you want to send this text to the input field, then you can add these lines yo your code:
input_field = driver.find_element_by_class_name('cc-m-form-view-input-wrapper').find_element_by_xpath('.//input')
input_field.send_keys(label_div_text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.