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()
Related
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(" ");
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.
Is it possible to insert a button element
(<button type= "submit">)
into the html code of the page using the driver.execute_script method?
And how to specify its location?
For example, there is a text field email (driver.find_element_by_xpath ("//input[#name= 'email']") and I need to insert the submit button under this field or above this field and click on it.
Selenium does not support such actions.
Selenium is designed to perform actions a real user can do with GUI.
As a user I can't add element into HTML web page.
I can click on element, get element text, scroll page etc.
Basically Selenium is used to testing. So, as per my suggestion there is no need to add anything from Selenium.
If there is no submit button, it is surely bug on your page.
So, we suggest you to check once again your page.
Selenium website link 🔗 https://www.selenium.dev/
The direct answer is No.
But through js you can update an attribute in Selenium. See below.
foo-text
you can basically update this foo-text to any other string, like :
driver.execute_script("document.getElementById('foo-id').innerHTML = '201';");
also, if you see we have id as an attribute that has an value foo-id, in case you want to update that id to someotherfoo-id, you can do as following :-
element = driver.find_element_by_class_name("foo-class");
driver.execute_script("arguments[0].setAttribute('id', 'someotherfoo-id')", element);
after this action, you must see the changes in UI. so basically you can do the modification like this.
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))
I am testing a JQuery Web application. I have a JQuery popup that I simply cannot get the submit button to fire in Watir. The same pattern is used thought the application.
I have verified the button exists and have tried click, fireEvent you name it and am out of methods to call.
Has anyone solved this?
Which do you want ?
A. Control cursor and click "submit"
or
B. Simula te click "submit"
A:need to use Autoit and control cursor and click ,but only for windows OS. B:execute the javascript that when clicking "submit".
If B case,there is two ways I used always.
1.execute that code in URL-bar.
ex.) #ie.link(:URL, 'javascript:<-CODE->;').click
or
Make like that module and use it by include in test case.
ex.) #ie.excute_script(<-CODE->)
module Watir
class IE
def execute_script(scriptCode)
WIN32OLE.codepage = WIN32OLE::CP_UTF8
window.execScript(scriptCode)
end
def window
WIN32OLE.codepage = WIN32OLE::CP_UTF8
ie.Document.parentWindow
end
end
Maybe...
I hope it help.
Sorry my poor english.
tknv/
In my case the solution was to use and index value of 1. JQuery creates a copy of the form and all the popup items have an index of 1. The 0 index controls are on the original form.
I just had the same problem. Ajax upload for jQuery demo is an example page. I want to upload a file using Upload button on the left hand side of the page.
When I click Upload button by hand, file upload pop up appears.
When I click the button with Watir, nothing happens.
Gary (accepted answer) helped me to find a solution:
browser.file_field(:index, 1).set "/path/to/file"