selenium with firefox close tab by javascript but SetTimeout() not ok - selenium

I use multithreaded way to open tab and when page finish load close tab
this is my open tab code
((JavascriptExecutor)webDriver).executeScript("window.open('"+url+"')");
When close tab,i iterate webDriver.getWindowHandles() and use function executeScript,but find some error,this is my code
this code work ok in chrome,it close all tab,but in firefox some windowHandler are closed and some not
((JavascriptExecutor) webDriver).executeScript("setTimeout(function(){window.close()},10000)");
or
((JavascriptExecutor) webDriver).executeScript("setInterval(function(){window.close()},10000)");
I try to use this code, but it's also like above code
((JavascriptExecutor) webDriver).executeScript("var script = document.createElement('script');\n" +
"script.type = 'text/javascript';\n" +
"script.innerHTML = 'setTimeout(function(){window.close()},10000);';\n" +
"script.innerHTML = 'window.close()';\n" +
"document.body.appendChild(script);");
but when i delete "script.innerHTML = 'setTimeout(function(){window.close()},10000);';\n" + only use window.close() it work close all tab, but does not meet the needs
so i think maybe setTimeout and setInterval function cause this problem in firefox
then i change code to this
((JavascriptExecutor) webDriver).executeScript("var start = null;\n" +
"\n" +
"function step(timestamp) {\n" +
" if (!start) start = timestamp;\n" +
" var progress = timestamp - start;\n" +
" if (progress < 2000) {\n" +
" window.requestAnimationFrame(step);\n" +
" } else {\n" +
" window.close();\n" +
" }\n" +
"}\n" +
"\n" +
"window.requestAnimationFrame(step);");
it's also some windowHandler are closed and some not
i don't know how can i close tab in firefox by javascript
The requirement is to close the tab directly after 10 seconds, or close the tab after the page is loaded

I'm not sure I fully understand the question but it seems you are trying to open a new tab, run some code, then close 10 seconds after it was opened?
I've tested this in firefox and it works with multiple tabs and each one closes on it's own.
((JavascriptExecutor) webDriver).executeScript(
"function openAndClose(url, timeout){" +
" var tab = window.open(url);" +
" setTimeout(function(){tab.close()},timeout);" +
"}" +
"openAndClose('https://www.google.com', 3000);");
If this is not it, could you explain your problem a little more clearly.

Related

Selenium not clicking on LIKE button on Instagram

For some reason, the Instagram bot seems to work but selenium is not clicking on the Like button. What may be the issue? Thank you.
unique_photos = len(pic_hrefs)
for pic_href in pic_hrefs:
driver.get(pic_href)
time.sleep(2)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
time.sleep(random.randint(2, 4))
like_button = lambda: driver.find_element_by_xpath('//span[#aria-label="Like"]').click()
like_button().click()
print('Liked!')
for second in reversed(range(0, random.randint(18, 28))):
print_same_line("#" + hashtag + ': unique photos left: ' + str(unique_photos)
+ " | Sleeping " + str(second))
time.sleep(1)
except Exception as e:
time.sleep(2)
unique_photos -= 1
try this :
//article[1]//div[2]//section[1]//span[1]//button[1]//*[local-name()='svg']
and if you want to click on all like button then you need to use below xpath undoubtedly you need list to handle it ::
//article[*]//div[2]//section[1]//span[1]//button[1]//*[local-name()='svg']

LiveCycle dynamic PDF - how to enforce Acrobat version?

I have a dynamic PDF that doesn't display correctly in Firefox, Chrome, or lower versions of Adobe. Is there a way to show blank page with error message when the user opens it with anything less than Acrobat version X?
I tried searching on this site and Googling but couldn't find anything..
Help much appreciated!!
This can be achieved with a PDF cover page but can also be achieved with your own subform combo and a bit of script. Creating a cover page that is hidden only to reveal your form content if a script executes correctly and is inside a full feature XFA form viewer is one way to tackle this.
function detect(){
//Sample platform detection
var viewerType = xfa.host.appType;
var versionNo = xfa.host.version;
var variation = xfa.host.variation;
var plugIns = app.plugIns;
var nplugIns = plugIns.length;
var msg = "";
if((viewerType == "Reader") && (nplugIns > 0)){
msg = "This PDF was opened in Reader " + versionNo + ", " + variation + ", and loaded " + nplugIns + " plug-ins.";
form1.Top.Cover.presence = "hidden";
form1.Top.Content.presence = "visible";
}
else if((viewerType == "Exchange") && (nplugIns > 0)){
msg = "This PDF was opened in Acrobat " + versionNo + ", " + variation + ", and loaded " + nplugIns + " plug-ins.";
form1.Top.Cover.presence = "hidden";
form1.Top.Content.presence = "visible";
}
else if((viewerType == "Exchange-Pro") && (nplugIns > 0)){
msg = "This PDF was opened in Acrobat Pro " + versionNo + ", " + variation + ", and loaded " + nplugIns + " plug-ins.";
form1.Top.Cover.presence = "hidden";
form1.Top.Content.presence = "visible";
}
else{
msg = "This PDF was opened in an unrecognized platform";
}
xfa.host.messageBox(msg); //Throw prompt.
form1.Top.Content.TextField1.rawValue = msg; //Write message to form.
}
Most of the function above relies on xfa.host.appType but some PDF viewers developped using the Adobe Acrobat code base will return valid platform values but will not load any plug-ins which is a way to detect and unsupported platform.

How to make a clickable link that executes a command with bukkit

I'm trying to make a bukkit plugin and I can't seem to find any documentation on this but I've seen it done, How would I input commands into a chat message that a user could click on to execute a command on the server like "/motd" in the form of a clickable link like a URL
if (commandLabel.equalsIgnoreCase("cmd") {
player.sendMessage("Pick a command: " + </motd> + ", " + </mail> );
}
replacing "" and "" to output something like this:
Pick a command: MOTD, Mail
and clicking them would execute the command to the server as them. How would I do this?
You could do it like this:
IChatBaseComponent comp = ChatSerializer
.a("{\"text\":\"" + "Choose one: " + "\",\"extra\":[{\"text\":\"" + "MOTD" + "\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + "/motd" + "\"}}]}");
PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
((CraftPlayer) <player>).getHandle().playerConnection.sendPacket(packet);
This would send them a message showing:
Choose one: MOTD
and when the user clicked MOTD, it would run the command /motd as the player. Here's a little breakdown of what we're actually doing:
IChatBaseComponent comp = ChatSerializer
.a("{\"text\":\"" + "<Ignored Message> " +
"\",\"extra\":[{\"text\":\"" + "<Message that will be clicked>" +
"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
"<Command to be run when message is clicked>" + "\"}}]}");
PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
((CraftPlayer) <player>).getHandle().playerConnection.sendPacket(packet);
The above code will send the player:
<Ignored Message> <Message that will be clicked>
and when the player clicks <Message that will be clicked>
they will run the command <Command to be run when a message is clicked>, and because it does not start with the command prefix, /, it will force them to chat <Command to be run when a message is clicked>.
Unfortunately, as far as I know, you can only put one click event per message, so you would have to do something like this:
Choose one:
MOTD
Mail
So you would have to do, where the variable player is the player:
player.sendMessage("Choose one:");
IChatBaseComponent comp = ChatSerializer
.a("{\"text\":\"" +
"\",\"extra\":[{\"text\":\"" + "MOTD" +
"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
"/motd" + "\"}}]}");
PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
IChatBaseComponent comp2 = ChatSerializer
.a("{\"text\":\"" +
"\",\"extra\":[{\"text\":\"" + "Mail" +
"\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" +
"/mail" + "\"}}]}");
PacketPlayOutChat packet2 = new PacketPlayOutChat(comp2, true);
((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet2);
When MOTD is clicked, /motd will be run by the player, and when Mail is clicked, /mail will be run.
Just as a side note, you will need to include craftbukkit in your build path, along with bukkit to do this
Or you could simply just do this (I did my own, You can edit it)
/execute #a ~ ~ ~ tellraw #p ["",{"text":"Click this to die","color":"dark_red","bold":true,"clickEvent":{"action":"run_command","value":"/kill #p"},"hoverEvent":{"action":"show_text","value":{"text":"","extra":[{"text":"Kills you!"}]}}}]
run_command can be replaced with Open URL too.
You can replace dark red with any colour too. You can replace true with false for bold if you want, /kill #p can be replaced with a command (Or a https:// link if you do Open URL, show_text can be replaced with Show Item, Show entity, or Show Achivement. Text & Kills you can be replaced with the different thing (eg, Show entity) (Entity replaces text)
I found a website if your stuck! Good day :) http://minecraftjson.com/

How to add line break to labels on axis for IE9?

I'm using Dojo 1.9.1.
I'm using dojox.charting to draw some charts.
I am using a labelFunc to produce a date/time label for my X-axis. This is working fine in all browsers.
But I want to add a line break to my label so that the date sits above the time, e.g.:
10/01/2014
06:00
I can add a html break tag to the string returned and this works in Chrome and Firefox but not IE9 (to be expected).
Has anybody solved how to do this one that works across all browsers including IE9 (or specifically ones that don't "do" html labels).
Cheers
Ian
My label func:
_labelFull: function (index) {
// Returns a label in the form "dd/mm/yyyy hh:mm" for tooltip labelling
var dt,
d;
dt = new Date(Date.parse(myglobalStartDateTime));
dt.setHours(dt.getHours() + Number(index));
// Full date/time
d = ("0" + dt.getDate()).slice(-2) + '/' +
("0" + (dt.getMonth() + 1)).slice(-2) + '/' +
dt.getFullYear() + ' ' +
("0" + dt.getHours()).slice(-2) + ':' +
("0" + dt.getMinutes()).slice(-2) + 'UTC';
return d;
}

how to take screenshots and paste it in a word /text file oneafter other in selenium rc &webdriver?

selenium.captureScreenshot("/tmp/" + this.getClass().getName() + "."
+ testMethodName + ".png");
selenium.captureEntirePageScreenShotToString();
selenium.captureEntirePageScreenshot("C:\screenshot.png","");
I tried all the above but I need some way to paste the screen shots in a text/document page one after other.