How to find button element with selenium webdriver? - javascript

I have the following code for a button :
<div class="header__login">
<button class="header__login-btn"> Login or Sign Up</button>
</div>
I have tried:
await driver.findElement(By.css('button.header__login-btn')).click();
await driver.findElement(By.className('header__login-btn')).click();
Neither of these works. It did not find the button.
Does anyone have a suggestion on how to get this to work?

Try using
await driver.findElement(By.cssSelector('button.header__login-btn')).click();
instead of
await driver.findElement(By.css('button.header__login-btn')).click();

Related

Click button in webpage with Javascript

Currently, I'm doing an application and I need login to this one particular website. Basically, I need to make this button click using a javascript but I have no idea how to.
This is the button code I retrieved from the website:
<button type="submit" class="uk-width-1-1 uk-button uk-button-primary uk-button-large">Sign in</button>
Thank you for your help in advance
You can click on the button programmatically, in case of pure javascript :
(function(){document.querySelector('button[type="submit"]').click();})()
In case of Android JS Interface :
webView.loadUrl("javascript:(function(){"+
"l=document.querySelector('button[type=\"submit\"]');"+
"e=document.createEvent('HTMLEvents');"+
"e.initEvent('click',true,true);"+
"l.dispatchEvent(e);"+
"})()");
The general way to have button click events is to use something like jQuery. For example lets set the button to have an ID
<button id="my-button" type="submit" class="uk-width-1-1 uk-button uk-button primary uk-button-large">Sign in</button>
Then we would apply a click event to it with jQuery
$('#my-button').click(function(){alert('button has been clicked');})
Instead of the alert you could put whatever code you want in there :D
Example: Codepen example
EDIT: Example for a class:
Example for a class
(Note that we use a "." instead of "#" in the call)
-Kyle

Python webdriver Htmlunit // Find_element_by_id make me an error

I would like Find_element_by_id on paypal in order to confirm the checkout, but I'm getting an error.
I use a working code with webdriver Chrome and Opera:
confirmButton = browser.wait.until(
EC.presence_of_element_located((By.ID, "confirmButtonTop"))
)
confirmButton.click()
I want to use it with HTMLUnit and PhantomJS, but it doesn't work. I've tried using a different method but nothing:
try:
WebDriverWait(driver, delay).until(EC.presence_of_element_located(
driver.find_element_by_id('confirmButtonTop'))
)
print "Page is driver!"
except TimeoutException:
print "Loading took too much time!"
or:
confirmButton = driver.find_element_by_id("confirmButtonTop")
confirmButton.click()
same error :
Unable to locate element with ID: confirmButtonTop
HTML:
<div id="button" class="buttons reviewButton">
<input track-submit="" type="submit" value="Continuer" id="confirmButtonTop"
class="btn full confirmButton continueButton" validate-submit="onPay()">
</div>

Unable to click on the submit button with xpath and classname using selenium webdriver

This is the html code :
...
<div class="span_3_of_4">
<p class="text_popup"> Dont Have an Account? |
<a class="fancybox" href="#load_box">Signup</a>
</p>
</div>
<div class="span_1_of_4" align="center">
<input class="button" type="submit" value="Submit"/>
</div>
</div>
</form>
</div>
<script src="js/jquery.form.js" type="text/javascript"/>
Its a submit button. I am trying to automate a registration for using selenium webdriver. There are no ids or names as such for this button. Hence i tried with xpath (taken from firebug) .//*[#id='load_form']/div/div[2]/input and classname - button.
Yet the following error was being thrown Element is not currently visible and so may not be interacted withCommand duration or timeout: 428 milliseconds.
Please suggest as to how can i ever overcome this error and be able to click on the "submit" button.
Selenium Version - 2.44
I encounter this issue a lot, and one thing that normally solves the problem is switching frames. With python I think it's something like driver.switch_to_frame('frame') I may be wrong there with the syntax but definitely try that out
Try this one:
driver.findElement(By.xpath("//div[#id='load_box']/form[#id='load_form']//input[#class='button' and #type='submit']"))
Problem was that there are several form elements with "load_form" on the site and the first one is hidden! that's why you need a more specific xpath like the one above.

AutoClick on Submit Button Javascript

This is whole code for this button <button type="submit" tabindex="46" class="btn-2 floatRight" onclick="_gaq.push(['_trackEvent', 'Rejestracja', 'Rejestracja - koszyk']);">Next</button>
I tried to put this _gaq.push(['_trackEvent', 'Rejestracja', 'Rejestracja - koszyk']); in GreaseMonkey to automatically proceed next step, but with no result.
Thanks for any ideas!
Have a nice day!
You're doing it wrong, you should instead find the button using document.querySelector() and then click it using the click() function, like this:
document.querySelector("button.btn-2").click()

Get the page URL for button

I'm trying to get the full page URl for my button, but i was not able to do that, how should i put the function into onclick?
<button onClick='window.location.href'>Button</button>
Any help would be appreciated.
This
<button onclick="javascript:alert(window.location.href)">Tell my URL?</button>
or this
<button onclick="javascript:console.log(window.location.href)">Log my URL?</button>
window.location.href is a string. setting the onClick to a string will do nothing, because you haven't told it to do anything.
For example, if you want onClick to alert() a string, this is how you'd do it:
<button onClick="alert(window.location.href)">Button</button>
Example: http://jsfiddle.net/vyyw3ynp/1/
Edit: printing via document.write()
<button onClick="document.write(window.location.href)">Button</button>
Example: http://jsfiddle.net/vyyw3ynp/3/

Categories

Resources