Waiting for a button state to change in Selenium Webdriver - python

I am testing a textbox and button - the user types in some information to the textbox and then hits the button - the button changes text from "Update" to "Updating" and then back to "Update" once the operation is complete.
I want Selenium to wait until the button returns back to "Update" before continuing.
I have tried various solutions using xpath here is where I am now:
self.wait.until(EC.element_to_be_clickable(By.XPATH, '//button[#id="send_data"][text()="Update"]'))
The HTML of the button is as follows:
<button id="send_data" type="button" class="btn btn-success" onclick="ChangeParam(this,'radio','freq'; return false;">Update</button>
and
<button id="send_data" type="button" class="btn btn-default" onclick="ChangeParam(this,'radio','freq'; return false;">Updating</button>
Where am I going wrong?

This is quite common issue: you should send type_of_locator, "locator_string" as tuple of arguments ((By.XPATH, "//button")), but not as two separate args (By.XPATH, "//button")
Use following code
self.wait.until(EC.element_to_be_clickable((By.XPATH,'//button[#id="send_data"][text()="Update"]')))

Another solution that should work is using EC.invisibility_of_element_located. After clicking Update button, you can wait invisibility of Element which represented of "Updating" state button.
self.wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR,'button#send_data .btn-default')))

Related

Result not refreshed or Click does not work (Selenium + Python)

Trying to get the result for the Website
I am not getting any error while clicking on Go button but the result also does not get refreshed. Can any one please help in this.
I tried few ways to click on Go but not working:
driver.find_element_by_xpath('//div[#class="col-sm-1 text-right"]/button[#class="btn btn-primary amfi-btn"]').click()
driver.find_element_by_xpath('//button[. = "Go"]').click()
driver.find_element_by_xpath(".//button[#class='btn btn-primary amfi-btn']").click()
driver.find_element_by_css_selector('[class="btn btn-primary amfi-btn"]').click()
driver.find_element_by_xpath('//button[normalize-space()="Go"]').click()
driver.find_element_by_xpath('//button[. = "Go"]').click()
driver.find_element_by_xpath(".//button[#class='btn btn-primary amfi-btn' and #type='submit']").click()

Validating Pin-Pad form using Selenium Python

I am trying to input code on a pin pad form available on a website and get it validated. However, I am getting a validation error. I fear the clicked pin buttons are not being read by the website's script. Can anyone help me with this? I am attaching the code as well as the page source code.
The UI of website looks like this
The div class on website which contains the keypad is as follows:-
<div class="pin_pad">
<span id='keypad1' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">1</span>
<span id='keypad2' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">2</span>
<span id='keypad3' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">3</span>
<span id='keypad4' onmouseout="this.className=''" onmouseover="this.className='pin_hover'">4</span>
<span class="submit_btn" onmouseout="this.className='submit_btn'" onmouseover="this.className='submit_btnpin_hover'" onclick="return validate();">Submit</span>
</div>
My pin is "4444". I am trying to use the .click() method to get "4" clicked. Although I can see that "4" has been clicked 4 times, upon submit, the page shows a validation error. Can anyone help me in solving this problem? Here's my code in python:-
pinpad = driver.find_elements_by_class_name('pin_pad')
element = driver.find_element_by_xpath("//span[text()=4]").click()
for k in range(4):
actionChains = ActionChains(driver)
actionChains.context_click(element).perform()
time.sleep(1)
driver.find_element_by_xpath("//span[text()='Submit']").submit()
Instead of
context_click(element).perform()
do this :
move_to_element(key4).click().perform()
context_click is for right click and move_to_element() is for moving your mouse cursor to the specify element.
Brief explanation :
You can introduce WebDriverWait for more stability, and I don't think you need to do context_click() instead it is move_to_element() plus ActionChains object creation optimization and instead of submit you may wanna use click(). Check out the code below :
pinpad = driver.find_elements_by_class_name('pin_pad')
key4 = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span#keypad4"))).click()
actionChains = ActionChains(driver)
for k in range(4):
actionChains.move_to_element(key4).click().perform()
sleep(1)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Submit']"))).click()

How to switch to a modal in Python using Selenium

I'm using Python and Selenium. My problem is that I can't switch on the modal that pops out and I can't click the buttons in it.
This is the elements of the modal:
This is my code:
minus the url of course
browser = webdriver.Chrome(executable_path="D:\\sasdsa\\automate\\chromedriver_win32\\chromedriver.exe")
user_name = browser.find_element_by_xpath("//input[#id='username']")
user_name.send_keys("test.employee")
##Password
pass_word = browser.find_element_by_xpath("//input[#id='password']")
pass_word.send_keys("123")
##log_in = browser.find_element_by_css_selector(".btn")
log_in = browser.find_element_by_xpath("//button[#class='btn btn-sm btn-primary btn-block']")
log_in.click()
##punch
#driver.find_element_by_id("//#id='product_view')
#To open the modal
punch_in = browser.find_element_by_xpath("//button[#class='btn btn-success btn-sm pull-right']")
punch_in.click()
#cant switch to the modal to access the button
browser.switch_to_frame("product_view")
punch_in2 = browser.find_element_by_xpath("//button[#id='save_me']")
punch_in2.click()
Delete the line below and it should work fine.
browser.switch_to_frame("product_view")
You don't need to do anything special here. A modal dialog like this is just HTML like any other HTML on the page. You access it just like you would anything else.
Having said that... if you click a button, etc. that launches the dialog, you will probably have to add a WebDriverWait to wait for the dialog to be visible before accessing elements inside it, etc.
Andersson and JeffC are right. I had a similar issue. I treated the modal window as something different, which didn't work by the way. At the end, I just treated it in the usual way. I simply added browser.implicitly_wait(60) after initiating the browser, and it worked.
What Jeff said worked for me. I had
<div class="ReactModalPortal">
I just added sleep for 3 seconds to see if it was working. It worked.
Then I used :
act = ActionChains(self.driver)
act.send_keys(Keys.TAB).perform()
act.send_keys(Keys.ENTER).perform()

Radio Button Click with Requests

I have been working on a web scraping script that gets past the login page but I can't find a way to get this radio button selected as the script attempts to login.
Here's the button:
<input type="radio" name="UserType" id="UserType" value="PARENTSWEB-PARENT" tabindex="4">
When it is clicked it looks like this:
<input type="radio" name="UserType" id="UserType" value="PARENTSWEB-PARENT" tabindex="4" checked="checked">
I am using a dictionary to submit the username and password but not sure how to add the button functionality into it.
Dict:
payload={
'username':USERNAME,
'password':PASSWORD,
'DistrictCode':DistCode,
'PARENTSWEB-STUDENT':'checked'
}
I am using the requests library and lxml to submit and look at the data. But if there is a better library or another one I can also use I'm open to anything.

Canot click button with Python with Selenium

I am trying to click a button that brings up a dialogue box to select a file. inspecting the element it looks like its an input rather than a button. Either way I cannot click it with:
element = browser.find_element_by_id("fileupload")
element.click()
and
browser.find_element_by_id("fileupload").send_keys("\n")
Neither of which seem to work.
Here is what I see when I inspect that element on the page:
<span class="btn btn-success fileinput-button">
<span class="glyphicon glyphicon-upload"></span>
Select and Upload...
<input id="fileupload" name="upfile" accept=".xml" type="file">
</span>
Any assistance help or pointers would be appreciated!
Clicking on a file input usually triggers a file upload dialog. Since you cannot control it with selenium, you need to avoid the dialog being opened by sending keys to the input instead:
browser.find_element_by_id("fileupload").send_keys("path_to_the_file")
See also:
How to deal with file uploading in test automation using selenium or webdriver
How to upload file using Selenium WebDriver in Java
How to upload file ( picture ) with selenium, python

Categories