I'm trying to click specific button but with no result yet.
Using Python 3.4.2 and Ghost.py.
<a class="button" title="" ref="1" id="details" href="javascript:void(0);">
</a>
This code is under many div's and li's.
The simplest answer is welcomed!
You can evaluate a line of Javascript with Ghost.py and use the click method on the appropriate DOM element you get with getElementById:
page, resources = ghost.evaluate("document.getElementById('details').click();", expect_loading=True)
UPDATE
To get the link by class use the following line
ghost.evaluate("document.getElementsByClassName('button')[0].click();", expect_loading=True)
There is another version you can use to select and click the first link with an ref="1" attribute on your page:
ghost.evaluate("document.querySelector('a[ref="1"]').click();", expect_loading=True)
Related
I am doing the following:
<a href="www.stackoverflow.com">
<button disabled="disabled" >ABC</button>
</a>
This works good but I get a HTML5 validation error that says "Element 'button' must not be nested within element 'a button'.
Can anyone give me advice on what I should do?
No, it isn't valid HTML5 according to the HTML5 Spec Document from W3C:
Content model: Transparent, but there must be no interactive content descendant.
The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).
In other words, you can nest any elements inside an <a> except the following:
<a>
<audio> (if the controls attribute is present)
<button>
<details>
<embed>
<iframe>
<img> (if the usemap attribute is present)
<input> (if the type attribute is not in the hidden state)
<keygen>
<label>
<menu> (if the type attribute is in the toolbar state)
<object> (if the usemap attribute is present)
<select>
<textarea>
<video> (if the controls attribute is present)
If you are trying to have a button that links to somewhere, wrap that button inside a <form> tag as such:
<form style="display: inline" action="http://example.com/" method="get">
<button>Visit Website</button>
</form>
However, if your <button> tag is styled using CSS and doesn't look like the system's widget... Do yourself a favor, create a new class for your <a> tag and style it the same way.
If you're using Bootstrap 3, this works quite well
Primary link
Link
I've just jumped into the same issue and I solved it substituting 'button' tag to 'span' tag. In my case I'm using bootstrap. This is how it looks like:
<a href="#register">
<span class="btn btn-default btn-lg">
Subscribe
</span>
</a>
No.
The following solution relies on JavaScript.
<button type="button" onclick="location.href='http://www.stackoverflow.com'">ABC</button>
If the button is to be placed inside an existing <form> with method="post", then ensure the button has the attribute type="button" otherwise the button will submit the POST operation. In this way you can have a <form> that contains a mixture of GET and POST operation buttons.
It would be really weird if that was valid, and I would expect it to be invalid. What should it mean to have one clickable element inside of another clickable element? Which is it -- a button, or a link?
These days even if the spec doesn't allow it, it "seems" to still work to embed the button within a <a href...><button ...></a> tag, FWIW...
Another option is to use the onclick attribute of the button:
<button disabled="disabled" onClick="location.href='www.stackoverflow.com'" >ABC</button>
This works, however, the user won't see the link displayed on hover as they would if it were inside the element.
You can add a class to the button and put some script redirecting it.
I do it this way:
<button class='buttonClass'>button name</button>
<script>
$(".buttonClass').click(function(){
window.location.href = "http://stackoverflow.com";
});
</script>
why not..you can also embeded picture on button as well
<FORM method = "POST" action = "https://stackoverflow.com">
<button type="submit" name="Submit">
<img src="img/Att_hack.png" alt="Text">
</button>
</FORM>
Explanation and working solution here:
Howto: div with onclick inside another div with onclick javascript
by executing this script in your inner click handler:
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
It is illegal in HTML5 to embed a button element inside a link.
Better to use CSS on the default and :active (pressed) states:
body{background-color:#F0F0F0} /* JUST TO MAKE THE BORDER STAND OUT */
a.Button{padding:.1em .4em;color:#0000D0;background-color:#E0E0E0;font:normal 80% sans-serif;font-weight:700;border:2px #606060 solid;text-decoration:none}
a.Button:not(:active){border-left-color:#FFFFFF;border-top-color:#FFFFFF}
a.Button:active{border-right-color:#FFFFFF;border-bottom-color:#FFFFFF}
<p><a class="Button" href="www.stackoverflow.com">Click me<a>
Use formaction attribute inside the button
PS! It only works if your button type="submit"
<button type="submit" formaction="www.youraddress.com">Submit</button>
I am doing the following:
<a href="www.stackoverflow.com">
<button disabled="disabled" >ABC</button>
</a>
This works good but I get a HTML5 validation error that says "Element 'button' must not be nested within element 'a button'.
Can anyone give me advice on what I should do?
No, it isn't valid HTML5 according to the HTML5 Spec Document from W3C:
Content model: Transparent, but there must be no interactive content descendant.
The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).
In other words, you can nest any elements inside an <a> except the following:
<a>
<audio> (if the controls attribute is present)
<button>
<details>
<embed>
<iframe>
<img> (if the usemap attribute is present)
<input> (if the type attribute is not in the hidden state)
<keygen>
<label>
<menu> (if the type attribute is in the toolbar state)
<object> (if the usemap attribute is present)
<select>
<textarea>
<video> (if the controls attribute is present)
If you are trying to have a button that links to somewhere, wrap that button inside a <form> tag as such:
<form style="display: inline" action="http://example.com/" method="get">
<button>Visit Website</button>
</form>
However, if your <button> tag is styled using CSS and doesn't look like the system's widget... Do yourself a favor, create a new class for your <a> tag and style it the same way.
If you're using Bootstrap 3, this works quite well
Primary link
Link
I've just jumped into the same issue and I solved it substituting 'button' tag to 'span' tag. In my case I'm using bootstrap. This is how it looks like:
<a href="#register">
<span class="btn btn-default btn-lg">
Subscribe
</span>
</a>
No.
The following solution relies on JavaScript.
<button type="button" onclick="location.href='http://www.stackoverflow.com'">ABC</button>
If the button is to be placed inside an existing <form> with method="post", then ensure the button has the attribute type="button" otherwise the button will submit the POST operation. In this way you can have a <form> that contains a mixture of GET and POST operation buttons.
It would be really weird if that was valid, and I would expect it to be invalid. What should it mean to have one clickable element inside of another clickable element? Which is it -- a button, or a link?
These days even if the spec doesn't allow it, it "seems" to still work to embed the button within a <a href...><button ...></a> tag, FWIW...
Another option is to use the onclick attribute of the button:
<button disabled="disabled" onClick="location.href='www.stackoverflow.com'" >ABC</button>
This works, however, the user won't see the link displayed on hover as they would if it were inside the element.
You can add a class to the button and put some script redirecting it.
I do it this way:
<button class='buttonClass'>button name</button>
<script>
$(".buttonClass').click(function(){
window.location.href = "http://stackoverflow.com";
});
</script>
why not..you can also embeded picture on button as well
<FORM method = "POST" action = "https://stackoverflow.com">
<button type="submit" name="Submit">
<img src="img/Att_hack.png" alt="Text">
</button>
</FORM>
Explanation and working solution here:
Howto: div with onclick inside another div with onclick javascript
by executing this script in your inner click handler:
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
It is illegal in HTML5 to embed a button element inside a link.
Better to use CSS on the default and :active (pressed) states:
body{background-color:#F0F0F0} /* JUST TO MAKE THE BORDER STAND OUT */
a.Button{padding:.1em .4em;color:#0000D0;background-color:#E0E0E0;font:normal 80% sans-serif;font-weight:700;border:2px #606060 solid;text-decoration:none}
a.Button:not(:active){border-left-color:#FFFFFF;border-top-color:#FFFFFF}
a.Button:active{border-right-color:#FFFFFF;border-bottom-color:#FFFFFF}
<p><a class="Button" href="www.stackoverflow.com">Click me<a>
Use formaction attribute inside the button
PS! It only works if your button type="submit"
<button type="submit" formaction="www.youraddress.com">Submit</button>
I'm trying to get a button written in HTML to link to a separate JavaScript file when it is clicked, the button in question is coded below
<input type="button" id="clickme" value="Submit" />
I am using the Brackets code editor, and the js file I want to be linked is in the same project as my HTML code, if that helps at all
thanks in advance
Load the script into the page with a <script> element.
Keep your code in a function instead of simply running the whole thing with the script is loaded.
Find the button in the DOM (e.g. with getElementById) and bind the function as a click event listener with addEventListener).
If you mean link, I would use an <a> (anchor) tag which has an attribute href which is the reference. Therefore, you could use:
Link to JS
Or perhaps you meant the onclick attribute which would be:
<input type="button" id="clickme" onclick="myfunction()" value="Submit" />
However, as was pointed out, this is not best practice either.
jQuery.click() vs onClick
Provides some options and reinforces the idea that onclick is not the best way to trigger a Javascript function.
Just link your js script to a page by:
<script src="js/button.js"></script>
and then just call action in that scripts through the class or id.
<button id="btnShow"><h1 class="jumbotron-heading">Expand</h1></button>
<p class="p1">Test</p1>
That's a jQuery not js, but anyway a good example as I think
$('#btnShow').click(function(){
$('.p1').show();
});
I had one link initially. Now I received the requirement of showing button instead of a link for A/B testing. I have to make their ids same since I don't want to write a new test for the button.
To decide which to display I added two unique classes in the li item. This is working fine.
But on testing it starts failing, giving the message "Subscriber link is not available" although button is present there.
I thought that it might failing because of two same ids are there so i added two same classes and change the selector from id to class. But it still failing.
<li class="not_subscriber subscriber-text-link " style="display:none">
<a class="link-orange subscribe" href="" id="subscribe_link"> Subscribe </a>
</li>
<li class="not_subscriber subscriber-orange-btn " style="display:none">
<a class="btn-orange subscribe" href="" id="subscribe_link" > Subscribe </a>
</li>
#FindBy(css = ".subscribe")
#NoSuchElementDescription("Subscriber link is not available")
protected WebElement _subscribeLink;
Is there any way such that i dont have to write new test and change ids and test will start passing??
Can you please try below Xpath:-
//li[#class='not_subscriber subscriber-orange-btn ']/a[#class='btn-orange subscribe']
Hope it will help you :)
Change the selector to xpath. Almost every element has an xpath. And be sure to replace the double quotes to single quotes. It should be more reliable than class or id selectors.
I have a need to add 3 buttons to call the same javascript form, when I add more than one instance of the link, they all seem to fail except for the first instance. Is this possible?
<a class="button" href="" id="eventButton">Find out more</a>
<script type="text/javascript" src="https://gatherhere.com/js/leadform.js" id="gather-loader" data-locationid="7ppyugvm" data-trigger="eventButton"></script>
You only need one <script> tag per script, remember that ids on an element are unique, and therefore multiple buttons with the same ID will invalidate your markup, meaning only the first button will have the action assigned to it.
If you want 2 buttons to share the same action in Javascript, consider using a class instead of an ID