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
Related
Here are 3 sample elements on the page:
<label class="btn btn-primary"> <input type="radio" id="Q20_0_0-A27" name="Q20_0_0" autocomplete="off" value="27" class="sr-only" onchange="$('#Q20_0_0_change').val('1');wiz.reload();" aria-describedby="Q20_0_0Text" required=""> Compliant </label>
<label class="btn btn-primary"> <input type="radio" id="Q20_1_0-A27" name="Q20_1_0" autocomplete="off" value="27" class="sr-only" onchange="$('#Q20_1_0_change').val('1');wiz.reload();" aria-describedby="Q20_1_0Text" required=""> Compliant </label>
<label class="btn btn-primary"> <input type="radio" id="Q20_2_0-A27" name="Q20_2_0" autocomplete="off" value="27" class="sr-only" onchange="$('#Q20_2_0_change').val('1');wiz.reload();" aria-describedby="Q20_2_0Text" required=""> Compliant </label>
As you can see the IDs are in the form of Q20_*_0-A27, where * ranges from 0-15.
My aim is to click on these elements / buttons to 'activate them, which loads the page to change the clicked button into:
example with #1.
<label class="btn btn-primary active"> <input type="radio" id="Q20_0_0-A27" name="Q20_0_0" autocomplete="off" value="27" class="sr-only" onchange="$('#Q20_0_0_change').val('1');wiz.reload();" aria-describedby="Q20_0_0Text" required=""> Compliant </label>
Below is my code, and I can't seem to identify the right element
try:
# Find the link with the specified text and class
elements = driver.find_elements(By.CSS_SELECTOR, 'label.btn.btn-primary input[id^="Q20_"][id$="-A27"]')
print(f'Number of elements found: {len(elements)}')
for element in elements:
element_id = element.get_attribute('id')
print(f'Element ID: {element_id}')
element.click()
input("Press Enter to continue...")
except NoSuchElementException:
# The element was not found, so wait and try again
time.sleep(1)
print("retrying")
continue
As you can see the IDs are in the form of Q20_*_0-A27, where * ranges
from 0-15.
You can use the built-in range() function:
for num in range(0,28):
driver.find_element(By.CSS_SELECTOR, f'#Q20_{num}_0-A27')).click()
I haven't tested, but you might be able to use a more generic CSS selector altogether:
label.btn
Could not click the button English.
It says no such element exists.
Tried the following and Select method.
element = WebDriverWait(driver, 40).until(expected_conditions.element_to_be_clickable((By.XPATH, "/html/body/content/main/div/form")))
select = Select(driver.find_element_by_css_selector("label[for='language-MCQ'] span[class='me-2']"))
Code block:
<input class="form-check-input d-none lang-list" type="checkbox" id="language-MCQ" name="language_ids[]" value="1" wire:model="language_ids.0" wire:change="addLanguageQuestions(1)">
<label class="form-check-label d-flex flex-nowrap align-items-center rounded-pill btn btn-lg btn-lang-select text-white cursor-pointer" for="language-MCQ">
<span class="me-2">
English
</span>
</label>
<div class="form-check ps-0 me-3">
<input class="form-check-input d-none lang-list" type="checkbox" id="language-Kannada" name="language_ids[]" value="2" wire:model="language_ids.1" wire:change="addLanguageQuestions(2)">
<label class="form-check-label d-flex flex-nowrap align-items-center rounded-pill btn btn-lg btn-lang-select text-white cursor-pointer" for="language-Kannada">
<span class="me-2">
Kannada
</span>
</label>
</div>
Did you try this xpath :
//span[text()='English']
if you do not wish to use text()
//label[#for='language-MCQ']/span
also this looks wrong
select = Select(driver.find_element_by_css_selector("label[for='language-MCQ'] span[class='me-2']"))
Note that Select is only applicable for html select tag.
To minimize the possibility of any kind of exception it is recommended that your xpath should be pointed to the interactive element, In this case span or label is not interactive element but the input tag.
and your possible xpath solutions can be -
.//span[contains(text(),'English')]/ancestor::label/preceding-sibling::input
Or
.//input[#id='language-MCQ']
Or
.//label[contains(#class,'btn-lang-select')]/preceding-sibling::input[#value='1']
something like this should work -
element = WebDriverWait(driver, 40).until(expected_conditions.element_to_be_clickable((By.XPATH, ".//span[contains(text(),'English')]/ancestor::label/preceding-sibling::input")))
element.click()
If the above xpaths are not working with the explicit wait and expected conditions then check element should be in iframe.
I'm trying to launch into a platform with help of selenium chromedriver in python.
But the platform offers Demo and real accounts which offers multiple click buttons with same class name. Please see the code below
<input type="hidden" name="username" value="username"> == $0
<input type="hidden" name="password" value="password"> == $0
<button type="submit" class="btn btn-launch" ng-transclude=""><button class="platform-btn ng-scope">Launch platform</button></button>
<button class="platform-btn ng-scope">Launch Platform</button>
<input type="hidden" name="username" value="username_demo"> == $0
<input type="hidden" name="password" value="password"> == $0
<button type="submit" class="btn btn-launch" ng-transclude=""><button class="platform-btn ng-scope">Launch platform</button></button>
<button class="platform-btn ng-scope">Launch Platform</button>
Xpath code
Real
/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[1]/td[8]/tc-platform-launch/form/button/button
Demo
/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[3]/td[8]/tc-platform-launch/form/button/button
How can I launch demo and real account in selenium python???
you can directly find element by xpath .This would work if on clicking button opens a new tab
browser=webdriver.Chrome()
real=browser.find_element_by_xpath("/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[1]/td[8]/tc-platform-launch/form/button/button")
demo=browser.find_element_by_xpath("/html/body/ui-view/ui-view/div/div/div/div/div/ui-view/section/table/tbody/tr[3]/td[8]/tc-platform-launch/form/button/button")
real.click()
demo.click()
browser.switch_to_window(browser.window_handles[-1])#To work on demo
browser.switch_to_window(browser.window_handles[-2])#to Work on real
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")
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)