Trouble handling hidden element with selenium - python-3.x

Every time I run this code, I get an issue reaching to the targeted page. The site requires post request parameter to be filled in to reach the page where I am after. However, using get request it was good to go until it hits "Var4" parameter within my code. Inspecting element I could see that it indicates as hidden. If i left the hidden parameter blank then it redirects to another location. So, satisfying this thing to get to the targeted page is beyond my capability. Any suggestion will be appreciated.
from selenium import webdriver
driver = webdriver.Chrome(r"C:\Users\ar\Desktop\Chromedriver\chromedriver.exe")
driver.get('https://www.infocomm.org/cps/rde/xchg/infocomm/hs.xsl/memberdirectory.htm')
Var1='Professional Services Providers'
Var2='AUSTRALIA'
Var3='0'
Var4='1'
driver.find_element_by_xpath('//select[#name="mas_type"]').send_keys(Var1)
driver.find_element_by_xpath('//select[#name="mas_cntr"]').send_keys(Var2)
driver.find_element_by_xpath('//input[#name="OtherCriteria"]').send_keys(Var3)
driver.find_element_by_xpath('//input[#name="DoMemberSearch"]').send_keys(Var4)
driver.find_element_by_xpath('//input[#type="submit"]').click()
Element for the hidden stuffs which should be applicable for "Var4":
<form name="searchform" id="searchform" action="memberdirectory.htm" method="post" onsubmit="return Checkform();">
<input type="hidden" id="DoMemberSearch" name="DoMemberSearch" value="1">
<div class="login block-type-a block">

As workaround, you can try execute javascript with selenium.
For example, to unhide element
driver.execute_script("document.getElementById('DoMemberSearch').type = 'text';")
or set value directly
driver.execute_script("document.getElementById('DoMemberSearch').value = '%s';" % Var4)

you could not sendkeys to a hidden element, what you can do is to use javascript to send the value
probably something like this
driver.execute_script("document.getElementById('DoMemberSearch').value='1')

Related

Button element not interactable

I've been building an automated checkout script in Python 3.8 for the following website https://fasttimes.com.au/ (site built using Magento) and all buttons, drop downs, inputs etc have functioned up until this point. At the shipping selection stage however the button responsible for saving shipping details and allowing proceeding to billing stage, according to the python console reads; "Message: element not interactable".
I have tried using execute_script to directly interact with the site, this does not yield an error and says it's completed, but within the WebDriver the button hasn't actually been clicked and so proceedings to the next step of checkout do not take place. As for 'Action Chains'. they simply yield the error above, same as finding by XPATH or CSS_Selector. I've tried using the import 'Keys' and attempting to action the button with the ENTER key, as well as using the WebDriver substitute .send_keys(u'\ue007') to no avail. Before it's mentioned below I've also tried using waits, however maybe I'm doing them wrong?
E.g
element = WebDriverWait(driver, 15).until(
ec.presence_of_element_located((By.ID, "login:guest")))
Correct me if above line is incorrect formatting
Site's HTML is as follows:
<div class="buttons-set" id="shipping-method-buttons-container">
<p class="back-link"><small>« </small>Back</p>
<button type="button" class="button" onclick="shippingMethod.save()"><span><span>Continue</span></span></button>
<span id="shipping-method-please-wait" class="please-wait" style="display:none;">
<img src="https://static.fasttimes.com.au/skin/frontend/ultimo/default/images/opc-ajax-loader.gif" alt="Loading next step..." title="Loading next step..." class="v-middle"> Loading next step... </span>
</div>
My current code is:
driver.find_element_by_xpath("//*[#id='shipping-method-buttons-container']/button").send_keys(u'\ue007')
Any suggestions would be greatly appreciated. I believe the button that I'm trying to interact with is
<button type="button" class="button" onclick="shippingMethod.save()"><span><span>Continue</span></span></button>
...but I'm not too sure. I feel like I've at this point exhausted all possible avenues, but I refuse to believe this damned button isn't intractable! Thank you:)
As you've mentioned you want to proceed to Billing stage, I understand you are at step 1 of the checkout process. Am saying this because what you have identified with xpath //*[#id='shipping-method-buttons-container']/button is the button at step 4 Shipping Method, which is indeed not intractable yet.
You need to select the Checkout Method first, and carry on from there. Give this a try:
driver.find_element_by_id('login:guest').click() # select the "Checkout as Guest" radio button
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//*[#id='onepage-guest-register-button']")))
driver.find_element_by_xpath("//*[#id='onepage-guest-register-button']").click() # click Continue
Alternatively, if it's step 4 you're really stuck at:
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//*[#id='shipping-method-buttons-container']/button")))
driver.find_element_by_xpath("//*[#id='shipping-method-buttons-container']/button").click()

How to click toggle switch in webdriverIO

the HTML Element looks like
<div class="toggleButton" ng-class="{checked: settings.$storage.enabledApps[app.name]}" ng-click="settings.apps.toggleApp(app.name)"><input class="workplayEnabled switch" type="checkbox" ng-checked="settings.$storage.enabledApps[app.name]"></div>
i tried browser.click('.toggleButton .switch'),browser.click('.toggleButton'), browser.click(' .switch')
but it's not working
it throws the error as element not visible
whereas if i try browser.isSelected('.toggleButton .switch'),browser.isSelected('.toggleButton'), browser.isSelected(' .switch') it returns false
As it stands, it's really hard to pin-point your EXACT problem as you're doing multiple things wrong.
HTML breakdown:
that is HTML generated via AngularJS;
if you analyze the Angular attributes (ng-attributes/directives) you'll notice only your <div> has click-triggered functionality added (via ng-click="settings.apps.toggleApp(app.name)"), thus you only have to target the <div> element;
the <input> only has reactive behavior (if the toggle-button is checked, then it gets populated on ng-checked trigger);
Your problem: Now that we narrowed down the solution to clicking your <div>, we need to make sure your selector is returning the correct element.
open your Angular app, open the browser console & try seeing if your selector is working (e.g.: $(div.toggleButton)). If the CSS-selector returns more than an element($(div.toggleButton).length > 1), that means you have multiple <div>s with that class so you will have to find a more specific locator;
after you have found the correct locator for your <div>, I would add a .debug()(more info here) prior to your click in your WebdriverIO script so I could easily debug the scenario;
once in debug-mode, I would try with the same selector that found my element in the browser console.
!!! Note: .isSelected() will always return false in your scenario as you mentioned you failed to click the toggle switch, which is satisfying the ng-checked condition. You can verify this by manually selecting your toggle switch in debug-mode and then performing the .isSelected() command on your <input>. Then it should return true.

SharePoint Redirect after post

My SharePoint input tag is using an XSLT variable for the redirect. I would like to substitute it for a javascript function to determine the value of the users input. Is this possible from within this tag? If not is possible to run a javascript function from within as XSLT variable? The following article show that something like this is possible but I need an example. http://www.nothingbutsharepoint.com/2011/05/05/extending-the-dvwp-passing-xsl-variables-to-javascript-aspx/
Thank you in advance.
<input type="button" id="Submit" value=" Submit " style="width:100px" onclick="javascript: if(!PreSaveItem()) return false;{ddwrt:GenFireServerEvent(concat('__commit;__redirect={',$RedirectLoc,'}'))}"/>
Basically behind GenFireServerEvent is a __doPostBack function.
__doPostBack('elementgeneratedname','__redirect={' + value '}');
Take a look into the browser what code is generated and use it in your function. Most probably first argument is not the id of the button and is the name of the web part container (somthing like 'ctl00$PlaceHolderMain$WebPartId').

How to click on link element with specific text using watir?

I'm not able to click on text link 'Add' using watir:
PAGE:
<div id="divAdd" style="float: right">
<a onclick="SwitchView('2')" style="color: #1B56A7; cursor: pointer;">Add</a>
</div>
Watir CODE:
browser.link(:text =>"Add").click
EXCEPTION:
Unable to locate element, using {:tag_name=>["a"], :text=>"Add"}
Please help me how to handle this?
If the page has a lot of ajax and javascript going on, you may just have to wait a little bit for the client side code to finish rendering the page after it has been loaded from the browser.
Try this
browser.link(:text =>"Add").when_present.click
If that does not work, then make sure the item is not in a frame or something..
btw, if there is more than one link on the page with the text 'Add' then you may have to specify a container outside the link that lets you identify which link you want. eg.
browser.div(id: => "divAdd").link.when_present.click
If
This would be my way of doing it.
while browser.div(:class, 'containerDIV').a(:text, 'Add').exists? do
browser.div(:class, 'containerDIV').a(:text, 'Add').click
end

Firewatir: Firewatir scripts to select a item from the drop down

I am new to Watir automation testing and would like to get some help for the drop down.On our website we have a state drop down where you enter the first letter of the state (in my example C for California) and it narrows it down to the all states starting with C. Once you have the list you need to click on the correct state. But I am having difficulties selecting the correct state.
(Below is the html from our website:
<div class="x-form-field-wrap x-trigger-wrap-focus" id="ext-gen202" style="width: 166px;">
<input type="hidden" id="entityStateCode" name="entityStateCode" value="">
<input type="text" id="ext-comp-1005" autocomplete="off" size="24" class=" x-form-text x-form-field x-form-focus">
I used the following to automate the scenario but none of these are giving me what i am looking for:
#browser.text_field(:id,"ext-comp-1005").value=("CA")
#browser.text_field(:id,"ext-comp-1005").set("CA")
#browser.text_field(:id=> "ext-comp-1055",:index => 5).set "CA"
I really appreciate that if you can point me to the right direction.
Thanks
I ran into a similar situation before. In my situation there was a TABLE inside the DIV which had a separate row for each item in the dynamic drop down. So, if that's the case for you then you would need to use something like this to access the items:
#browser.text_field(:id,"ext-comp-1055").set "C"
table = #browser.div(:id, "ext-gen336").table(:index, 1)
puts "First entry value: #{table[1][1].text}"
table[2][1].click # second entry
Try printing out the HTML for the DIV at runtime to see the specifics of what you need to interact with if that doesn't work.
You did not tell what the problem is, this is not enough:
none of these are giving me what i am
looking for
This should enter CA into text field:
browser.text_field(:id => "ext-comp-1005").set("CA")
If it is entering text into wrong text field, change :id => "ext-comp-1005".
If it is entering text into the correct text field, but list of states does not appear, you probably have to fire some javascript event. Take a look at How to find out which JavaScript events fired? question.

Resources