How to manage a button with disabled attribute using Python Selenium? - javascript

Having this button:
<button id="btn-login-5" type="button" class="m-1 btn btn-warning" disabled="">Update</button>
I want to remove the disable attribute in order to click the button.
This is the code i see everytime:
button = self.driver.find_element(
By.XPATH, "/html/body/div/div/button")
self.driver.execute_script(
'arguments[0].removeAttribute("disabled");', button)
But I can't figure me out how that can work for anyone, I mean, if the element is disabled, selenium cannot run that very first line, is not able to asign the var "button" because he can't find that element.
Am I missing something? I am getting this error if a run that:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div/button"}
Update
I just realized this is related to selenium grid, I tried to click the button in a lot of ways, and no one works, but if instead of using selenium grid I use a local webdriver all of them works! So I have no idea what to do now.

If you use this relative xpath, instead of absolute one, it works:
button = driver.find_element(By.XPATH, "//button[#type='button']")
But is this the only button in your DOM? If not, you may have to add other relative entities to it.
Refactored Code (as per your query info provided):
button = driver.find_element(By.XPATH, "//button[#type='button']")
driver.execute_script('arguments[0].removeAttribute("disabled");', button)
button.click()
Output:
Process finished with exit code 0

The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button clickable again.
You were close enough. To remove the disabled attribute, the Update element can be still identified for it's presence using the following Locator Strategy:
button = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.XPATH, "//button[#class='m-1 btn btn-warning' and text()='Update']")))
self.driver.execute_script('arguments[0].removeAttribute("disabled");', button)

All works fine, the problem is a different one, my code couldn't find the element because the element was not there, something very strange, I have a docker with the page server and if I open the page I can see the button but the selenium grid webdriver doesn't load the button at all, I don't know why, maybe that button depends of some server wich is not accesible from the webdriver.
I'll ask the page developer for this, but the code was good :)
I lost a full day of work for nothing :)

If it's automated tests that fail, running with a --headless option, you could enable print-screens in case of errors and then check if the button shows or not. Might be that the button is outside of the viewport. Or the automated testing does not wait long enough for the element to be rendered.

Related

Selenium click is not working with this one "document.getElementById("submitMe").click()"

I'm facing issue while click on the submit button using selenium webdriver
also unable to click on the the web page button dynamically.
I'm using selenium chrome webdriver, have tried below options as well,
driver.find_element_by_xpath('//*[#id="get"]').click()
driver.execute_script('document.getElementById("submitMe").click()')
but still it won't help me to get resolve the issue.
can someone please help me to get it run or suggest me if you have any alternative?
Try grabbing the element reference through Selenium and then passing that as an argument the JavaScript executor to click()
element = driver.find_element_by_xpath("//*[#id='get']")
driver.execute_script("return arguments[0].click()", element);
I've never tried this, but if this is a link, button, or other form input, I think you could also focus the element and try to pass a space to it to emulate a keyboard interaction. It should act the same as "tabbing to the link and pressing space"
# grab element
element = driver.find_element_by_xpath("//*[#id='get']")
# focus
driver.execute_script("return arguments[0].focus()", element);
# send "space"
element.sendKeys(" ");

Selenium (python). Can't find correct way to click on button

There is html where I can't find correct way to click on button via selenium on python.
Here is the part of code
So, I need to click on
class="standart-wallet_standartWalletText__zjyj3"
via selenium but it seems nothing work.
What I tried:
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='standard wallet']")))
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[starts-with(#class,'standart-wallet_standartWalletText')]"
wait.until(EC.element_to_be_clickable((CSS_SELECTOR, ...)))
wait.until(EC.element_to_be_clickable((CLASS_NAME, ...)))
and so on...
What I got:
raise TimeoutException(message, screen, stacktrace)
So, please help ¯_(ツ)_/¯
The element is probably hidden, as it's nested in accordion. So it won't get clickable status until getting expanded. You can try to implement a function that expands it or simply use EC.presence_of_element_located() instead.

How to select option with Javascript from web browser developer tools

I am trying to select a desired value from dropdown menu in browser developer tools with Javascript. The following picture shows the content of the page.
But whenever I select another value with code and then click the save button, the changes are not actually saved. When I select normally with my mouse and click the save button, it changes normally.
I have tried the following stuff. These seemingly changed the value of selected dropdown menu (it shows 2 when I write the code) but cannot be saved afterwards.
1- document.getElementById('channel24g_ctrl')[2].selected = true
2- document.getElementById('channel24g_ctrl').options.selectedIndex = 2
3- document.getElementById('channel24g_ctrl').options[2].selected = true
4- document.getElementById('channel24g_ctrl').options[2].click()
5- document.getElementById('channel24g_ctrl').options[2].setAttribute("selected", "selected");
document.getElementById('channel24g_ctrl').options[1].removeAttribute("selected");
I might have tried some other combinations of those above.
Only the fifth actually makes target option "selected" but it still doesn't work when save button is clicked. I am no expert with javascript or ember but I have done similiar things before with other websites and succeeded. So maybe the problem is with Ember Framework?
What else can I do?
Apparently, selecting an option alone is sometimes not enough. change() must be called after selecting the option in order to apply changes. The following piece of code made the trick:
document.getElementById('channel24g_ctrl').options[2].selected = true;
$(document.querySelector('#channel24g_ctrl')).change();
document.getElementById('wifi_wizard_save').click();
document.getElementById('personlist').getElementsByTagName('option')[11].selected = 'selected'

JavaScript - Selenium Webdriver. How to check if element is visible

Im using Selenium to open a page. After I open the page, I want to click a some sort of refresh button. When I click it, Im getting this error:
ElementNotVisibleError: element not visible
This is the code I use to click the button:
driver.findElement(By.id(id)).click();
My guess is that the button goes not visible whenever it is pushed. And that maybe it starts off not visible as well. Cause it doesnt do a full page refresh.
Any way for selenium to check if something is visible, and maybe wait until it is?
Edit:
As it turns out, there are multiple buttons with the same ID. And the button I am trying to reach are way down. I tried to find the button with By.xpath, but I still couldnt find it. The way I did it, was to search for button by id, like this //button[contains(#id, 'abc')][1]. (Different number of course).
Selenium could not find the element, but I could find it with Chrome developer tools. Any suggestions?
Solution: 1 You can try using this code in a syso,
System.out.println("getting Page Source "+driver.getPageSource());
This method will return the entire page Source and you can check whether your button exists in the source or not. You can place the above code at several points in your function and check until you find the button in the source.
Solution: 2 In case your button does not exist in the source you will have to check and see whether the button is getting loaded in an iframe and if so you can use the following code and switch to the iframe and and then try finding the button,
driver.switchTo.frame(frameName);
In your case you could use Explicit wait to check when the element/button is clickable.
Try doing this
wait.until(ExpectedConditions.visibilityOfElementLocated(id))

Selenium - automating javascript button click

Test page
I'm using the above Indeed page as a test page for selenium. I want to automate clicking the 'apply' button so the form comes up using the Firefox webdriver. I have the following code.
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('http://www.indeed.ca/cmp/Belwood-Poultry-Ltd./jobs/General-Labourer-41a34df8c87843f8?sjdu=vQIlM60yK_PwYat7ToXhk0ht1loLTVg3_Mbro-8i_Oq8QnPfsih5TP4MoBFYzPbENZnyl4Z8fkU7srETrjh8IA')
self.mouse = webdriver.ActionChains(self.driver)
driver.find_element_by_class_name("indeed-apply").click()
There's no error generated but the apply button isn't clicked (nothing appears to happen). Obviously that HTML tag isn't the one attached to the javascript event that triggers the click and I'm not sure how to fix this.
Thank you
From what I see, the class name is not indeed-apply, but indeed-apply-button:
driver.find_element_by_class_name("indeed-apply-button").click()
There are two Apply Now button so if you want to click the first one see below:
driver.find_elements_by_css_selector(".indeed-apply-button")[0].click()

Categories