Powershell select ID element and click from web page - javascript

I am looking to open and click a button from a webpage in powershell.
The element I am selecting is:
<span class="x-btn-inner x-btn-inner-center" id="BtnExportCSV" unselectable="on">Export Dashboard as CSV File</span>
In the web console if I run the below code the button successfully clicks:
document.getElementById("BtnExportCSV").click();
The script I am running in powershell is below.
# open the specified web site and commit the key
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://wegpage.com/")
$ie.visible = $true
while($ie.busy) {sleep 1}
$doc = $ie.document
# commit the button
$commit = $doc.getElementById("BtnExportCSV")
if ($commit) { $commit.click() }
The webpage loads successfully, however the button does not click. Instead an error is produced:
You cannot call a method on a null-valued expression.
$commit = $doc.getElementByID("BtnExportCSV")
How do I get around this error? Any help greatly appreciated!

Lowercase the "d" in getElementByID("BtnExportCSV")
$commit = $doc.getElementById("BtnExportCSV")

Related

Twitter intent link not working on page, but it does work from dev tools

When I click on the link on the page, I receive the following error:
twitter.com is blocked twitter.com refused to connect ERR_BLOCKED_BY_RESPONSE
I use the following code to add the link to the page:
function getQuote() {
let newQuote = randomQuote().q;
let newAuthor = randomQuote().a;
let tweetURL = 'https://twitter.com/intent/tweet?hashtags=quotes&text=';
tweetURL = tweetURL + encodeURIComponent(newQuote + ' ' + newAuthor);
$('#text').text(newQuote);
$('#author').text(newAuthor);
$('#tweet-quote').attr('href', tweetURL);
}
If I open up the developer tools in chrome, I can click on the link in the tag and it works perfectly fine.
Why won't it work on the page?
I had the same issue with my React code. I also had target attribute set to "_blank".
Read the below post, changed it to "_top" and the tweet button started working on my chrome browser.
https://github.com/freeCodeCamp/freeCodeCamp/issues/40089

How to click button that is showed after scrolling with firefox webdriver in Python?

Hi my problem is this: I need to scroll a page and at the end of scrolling appear a button, a classic "Show More" button, when i click this button i need to re-scroll the web-page 'till arrive to the end of the web-page.
What i've done 'till now is this:
from contextlib import closing
import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver import Firefox # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
url = 'https://play.google.com/store/apps/category/GAME_ARCADE/collection/topselling_free?hl=it'
# use firefox to get page with javascript generated content
SCROLL_PAUSE_TIME = 0.5
capabilities = DesiredCapabilities.FIREFOX
capabilities["marionette"] = True
# Get scroll height
with closing(Firefox(capabilities=capabilities)) as browser:
browser.get(url)
last_height = browser.execute_script("return document.body.scrollHeight")
button = None
while True:
if (browser.find_element_by_id('show-more-button')):
browser.find_element_by_id('show-more-button').click()
#print("\n\nButton found = " + str(button) + "\n\n")
# Scroll down to bottom
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = browser.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# wait for the page to load
# store it to string variable
page_source = browser.page_source
print(page_source)
What i want to do with this code is to check a button element in the page, if not exists yet, scroll the page. If the button element has been found, click it, and scroll the page again 'till the end.
The problem of this code is when i try to click the found button i receive this error:
Traceback (most recent call last):
File "/Users/gfuccio/PycharmProjects/htmlParsing/generatedHtmlParser.py", line 21, in <module>
browser.find_element_by_id('show-more-button').click()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 77, in click
self._execute(Command.CLICK_ELEMENT)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
return self._parent.execute(command, params)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in execute
self.error_handler.check_response(response)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message:
I don't get why if the button is found, the click event doesn't work?
I hope that description of my problem is easy to understand.
Thanks in advance
EDIT
I've changed the if control in this way:
if (browser.find_element_by_id('show-more-button').is_displayed()):
print("\n\n\nDisplayed!!\n\n\n")
But it results always false, although the generated html, after scrolling, contains the following element:
<button class="play-button" id="show-more-button" style="display:none">Mostra altro</button>
I know that the problem is the style attribute, but with javascript i think that this value is changed, don't you think? Because if i scroll the page 'till i find the button, whether i inspect the page i find the following situation:
If you see the display attribute is setted to block. So why after scrolling i get the new html generated by javascript and the "style" attribute of the element is not changed?

closing browser within chrome page on test complete

So using TestComplete I'm essentially trying to open up a session on chrome, navigate to a web page, and then click a button on that page. after I'm finished I want to close that browser page. I'm having trouble closing the page though. Here is the code I have so far.
function ChromeTest
{
Browsers.Item(btChrome).Run(MyWebAdress);
var browser = Sys.Browser("chrome");
var page = Sys.Browser("chrome").Page(MyWebAdress);
var MyButton = page.ButtonLocation;
MyButton.click();
browser.BrowserWindow.Close(5000);
}
however, at the Close line I get an error that says "Unable to find the object BrowserWindow". Thanks in advance for any help you have.
Change BrowserWindow to BrowserWindow(0) (or whatever index you see in the Object Browser):
browser.BrowserWindow(0).Close(5000);
Or you can call Close() directly on the Chrome process:
browser.Close(5000);

Powershell to automate web site testing with modal dialog

Working on a quick Powershell script to use the IE object to enter values and submit a web page with a modal dialog. I want to be able to invoke the modal dialog, check some boxes on the dialog, and then close it and return to the page. I can invoke the modal dialog but nothing executes after the line to invoke the dialog until it is closed.
Powershell
$ie = new-object -com "InternetExplorer.Application"
$ie.visible = $true
$ie.navigate($url); do {sleep 1} until (-not ($ie.Busy))
$doc = $ie.Document
$books = $doc.getElementById(“searchBooks”).click();
Javascript
var ret = window.showModalDialog('selectBooks.aspx, books, ...);
I need a way to invoke commands in the context of the dialog. Tried running in the background and creating a runspace but no joy with either solution
& {$books = $doc.getElementById(“searchBooks”).click();}
(blocking)
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
$rs.SessionStateProxy.SetVariable("doc", $doc)
$p = $rs.CreatePipeline( { $books = $doc.getElementById(“searchBooks”).click(); } )
$p.Input.Close()
$p.InvokeAsync()
(not blocking, but I didn't have a handle to dialog elements)
That's an expected behavior of the modal dialog. If you want to interact with a dialog, perhaps showModelessDialog() is what you want.

Open a url in Windows Metro App via Javascript

Normally, when I use in my metro application, the url is opening in the default web browser. I don't want to do this with anchor, I want to do the same behavior via Javascript and as async. But I don't know how to open the url with default browser.
Here is my code:
var $aTag = $("<a/>").click(function (event) {
showYesNoDialog(
"Do you approve?",
"The link will be opened in another window. Do you approve?",
"Yes", // Text of yes button
"No", // Text of no button
function () { // Behavior of yes button
// I tried this but nothing happened.
window.location.href = _URL_; // Should open in chrome or default web browser
},
function () { // Behavior of no button
// do nothing
}
);
});
What I also tried is that:
$("<a href='" + _URL_ + "'></a>").click();
But this didn't work, too.
Finally, I found my answer while searching on google.
Open a URL in a new tab (and not a new window) using JavaScript
I used this code to open the url out the metro app and it worked in my situation:
window.open(_URL_, '_blank');
window.focus();
You cannot launch an actual application from within Metro, but what you can do is launch a file with associated program, and that should give you the functionality you need.
Check Sample
The sample covers files and URI's - http://msdn.microsoft.com/library/windows/apps/Hh701476

Categories