Python Selenium Multiple buttons with same class - python

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

Related

Trying to identify the elements below, with differing IDs (Selenium & Python) using CSS selector or XPATH

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

automate filling in web forms

There is a text box (ID) on the website and I want to put 10,000 data in it. After placing each data in this text box, results are displayed in other text boxes according to the ID.
Textbox ID :
<input class="form-control ltr left text-box single-line" data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" maxlength="10" name="NId" onblur="LoadInfo()" type="text" value="">
Textobx Name (fill after enter the ID in text box) :
<form action="/Employees/Manager/SavePerson" data-ajax="true" data-ajax-begin="onpostcreatebegin" data-ajax-complete="onpostcreatecomplete" data-ajax-loading="#ajaxloading" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#result" id="form0" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="asdxBadsP7CpS53654as6dadH3865asdadKhjasdad"> <input type="hidden" name="empId" id="empid" value="0">
<div class="panel panel-info">
<div class="panel-body">
<input data-val="true" data-val-regex="It is 10 numbers" data-val-regex-pattern="^[0-9]{10}$" data-val-required="*required" id="NId" name="NId" type="hidden" value="1234567890">
<input data-val="true" data-val-number="The field PersonId must be a number." data-val-required="The PersonId field is required." id="PersonId" name="PersonId" type="hidden" value="254102232">
<input data-val="true" data-val-required="The GuidId field is required." id="GuidId" name="GuidId" type="hidden" value="665xs6asd-cxc2-wq56-8888-30654998b166">
<div class="form-group">
<div class="col-md-4">
<input class="form-control text-box single-line" data-val="true" data-val-regex="enter the name." data-val-regex-pattern="^[\u0600-\u06ff\s]+$|[\u0750-\u077f\s]+$|[\ufb50-\ufc3f\s]+$|[\ufe70-\ufefc\s]+$|[\u06cc\s]+$|[\u067e\s]+$|[\u06af\s]$|[\u0691\s]+$|^$" data-val-required="*required" id="Firstname" name="Firstname" type="text" value="jack">
<span class="field-validation-valid text-danger" data-valmsg-for="Firstname" data-valmsg-replace="true"></span>
</div>
</div>
</div>
</div>
</form>
The page will not reload after entering the value in the text box ID. Shows the value on that page. What solution do you suggest? I do not know which method to consider that is the most optimal method. Can you introduce the most optimal and practical method?
Depending on how complex the task is, you can use either Selenium (Selenium is a script-controlled Browser) with Firefox WebDriver, for example.
It is of course also possible to use the request methods GET and POST as a method with more work but much higher performance.

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

html dropdown containing list python output

I am trying to use the list from my python output into a dropdown menu in an HTML form.
I have a website built in HTML/ CCS, a server with nodeJS, and several scripts i want to use in python3.
I currently have no idea how to integrate the python output to the dropdown. Do i need to use Javascript or my node server is enough?
My HTML form
<form action="/LUNProvisionning" method="post">
<fieldset>
<legend>LUN Provisionning</legend>
<br />
<label>Choix du serveur : </label>
<select id="serveur" name="serveur">
# put python output here!!
<option value="1">no serveurs</option>
</select>
<br />
<label>Volumétrie en Go 5 - 1000 : </label>
<input type="number" min="5" max="1000" placeholder="500" name="volumetrie" required>
<br />
<label>Type de provisionning : </label>
<select id="provisionning" name="type" required>
<option value="Thin">Thin</option>
<option value="Deco">Deco</option>
</select>
<br/>
<input type="submit" value="Submit">
</fieldset>
</form>
python output:
SSD_r1
SSD_r5
SSD_r6
fs_cpg
CPG4S2
These need to be in the dropdown above.
I have a node server that i can show if it's relevant.
Thank you very much for your help.
Any informations or links, anything can help.

Friendly URL with get request form

i have this simple html form:
<form action="test/" method="get" accept-charset="utf-8">
<input type="text" name="first" value="" id="first">
<input type="text" name="second" value="" id="second">
<input type="text" name="third" value="" id="third">
<p><input type="submit" value="Continue →"></p>
</form>
when user click submit button, i want to get friendly URL like this:
www.site.com/test/first/second/third
how can i do this?
Where the user ends up after filling out the form and clicking "Continue" depends on what you set the action attribute in your form tag to. You've currently set it to test/.
If you want them to end up at test/<first_val>/<second_val>/<third_val>/, then you can either use some JavaScript (per pythonFoo's answer), or you can redirect in the view that test/ points to, using HttpResponseRedirect:
def test_view(request):
return HttpResponseRedirect('/test/%s/%s/%s/' % request.POST['first'],
request.POST['second'],
request.POST['third])
<input type="text" name="first" value="" id="first">
<input type="text" name="second" value="" id="second">
<input type="text" name="third" value="" id="third">
<p><input type="button" value="Continue →" onclick="submitFriendly();"></p>
<script type="text/javascript">
function submitFriendly() {
window.location.href = window.location.href + document.getElementById('first').value + '/' + document.getElementById('second').value + '/' + document.getElementById('third').value;
}
</script>
This should work. It doesn't check if all inputs are filled.

Categories