Seleniumvs Java Applet - python

I'm making a bot with selenium and IE to automatize some tasks. I'm now facing a problem that I don't know how to solve : there is a Java Applet and I need to click in a button that is inside this Applet, how could I do that ?

Try this example for python , i have removed the Java one:
from selenium import webdriver
wd = webdriver.Firefox()
wd.get("http://localhost")
wd.execute_script("function()")

Related

Running into errors following a selenium chromedriver web automation tutorial

I'm fairly new to programing and have been trying my best to get a grasp of different concepts. I've made a few games and twitter bots, and am looking at trying some web scraping/automation.
I have been following along with this tutorial https://www.youtube.com/watch?v=f7LEWxX4AVI
and have gotten stuck trying to enter text into the search bar. I'm using chrome Version 81.0.4044.129 and have the right Chromedriver to go with it.
This is my code from the tutorial -
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://youtube.com')
searchbox = driver.find_element_by_xpath('//*[#id="search"]')
searchbox.send_keys('Type Type Type')
searchButton = driver.find_element_by_xpath('//*[#id="search-icon-legacy"]')
searchButton.click()
It opens up chrome but does not enter anything into the search bar
📷
https://gyazo.com/68da5e2e2340265ae50125558ed8ed11
I get these errors that open in a new window
and I get these in my python shell
📷
https://gyazo.com/e9d702ef1ec4e4bc422ae537a3b59fbe
any advice would be very much appreciated.

How to Navigate Context Menus (Selenium, Python)

I am aware that Selenium apparently doesn't support navigating context menus. But I have also seen in several other threads that there is a work-around by using action chains. Using context_click() followed by arrow key commands to navigate through the menus.
All examples I've seen have used Java, and when I translated to Python, only the context_click() command would register. Strangely enough, I wouldn't get an error either. Other sources have said that the context menus Selenium produces are only system level, and thus, Selenium cannot touch them, only create.
So my question is, has anyone been able to successfully navigate and chose options from context menus through Selenium? Python examples are preferred, but I'll take any advice or answers I can get.
Edit:
Code:
driver.get('https://www.google.com/')
actionChains = ActionChains(driver)
actionChains.context_click().send_keys(Keys.ARROW_UP).send_keys(Keys.ENTER).perform()
Context:
This is just a test script that I have been running to test this situation. In my personal project, I need to navigate the context menu to access a chrome extension. Since selenium can only interact within the web page I can't have it click on the button for the Chrome extension that is displayed by the browser. So this is the work-around I have been attempting.
Research:
https://testingrepository.com/how-to-right-click-using-selenium-webdriver/
- This source tells that seleniums context menus are only system level. In Java examples they also use a .build() command. As far as my knowledge, this command is not available to Python.
Select an Option from the Right-Click Menu in Selenium Webdriver - Java
- Thread suggesting that arrow key commands should work. However, all examples use Java and the .build() command as well
https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/common/action_chains.py
- shows that ActionChains() are Pythons's version of a .build() command. Might be common knowledge to some. I did not know this prior.
How to perform right click using Selenium ChromeDriver?
- very similar question to mine. While one user suggests that the menu cannot be interacted with, another suggests the actionChains work-around will work.
Thin,
I had the same problem and wonder nobody answered this already... It wasn't possible for me to solve it with selenium, cause selenium would navigate within the page. My solution:
import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
ActionChains(driver).move_to_element(element).context_click().perform()
wsh.SendKeys("{DOWN}") # send the keys you want
Well we can solve this using selenium along with pyautogui. The reason for using pyautogui is that we need to have control of the mouse for controlling the options on the context menu. To demonstrate this, I am going to use a python code to automatically open a google image of Avengers Endgame in new tab.
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import pyautogui
URL = 'https://www.google.com/'
PATH = r'C:\Program Files (x86)\chromedriver.exe'
driver = webdriver.Chrome(PATH)
action = ActionChains(driver)
driver.get(URL)
search = driver.find_element_by_name('q')
search.send_keys('Avengers Endgame')
search.send_keys(Keys.RETURN)
image_tab = driver.find_element_by_xpath('//a[text()="Images"]')
image_tab.click()
required_image = driver.find_element_by_xpath('//a[#class="wXeWr islib nfEiy mM5pbd"]')
action.context_click(required_image).perform()
pyautogui.moveTo(120, 130, duration=1)
pyautogui.leftClick()
time.sleep(1)
pyautogui.moveTo(300,40)
pyautogui.leftClick()
Now in the above code, the part till pyautogui.moveTo(120, 130, duration=1) is selenium based. Your answer begins from pyautogui.moveTo(120, 130, duration=1) and what this does is simply moves the mouse button to the open image in new tab option of the context menu(Please note that the screen coordinates may vary based on your screen size). The next line clicks the option(using action.click().perform() won't work as expected).
The next two lines helps in navigating to the tab after it opens. Hope the code helps!
one way to work around this I think at least in java is to perform those actions with one instance of the class Robot.
This is just an example of handling authentication in chromme. Is very usefull when I need to perform actions out the selenium scope.
try{
Robot bot = new Robot();
for(char c: userArray){
bot.keyPress(c);
bot.keyRelease(c);
bot.delay(300);
}
bot.keyPress(KeyEvent.VK_TAB);
bot.keyRelease(KeyEvent.VK_TAB);
for(char c: userArray){
bot.keyPress(c);
bot.keyRelease(c);
bot.delay(300);
}
bot.keyPress(KeyEvent.VK_ENTER);
bot.keyRelease(KeyEvent.VK_ENTER);
}catch(Exception e){
e.printStackTrace();
}

Using AutoIT with Selenium

Thank you for answering my previous question but as one is solved another is found apparently.
Interacting with the flash game itself is now the problem. I have tried researching how to do it in Selenium but it can't be done. I've seen FlashSelenium, Sikuli, and AutoIT.
I can only find documentation for FlashSelenium in Java, It's easier for me to use AutoIT rather than Sikuli as I'd have to learn to use Jpython to create the kind of script I want to, which I am not straying away from learning just trying to finish this as fast as possible. As for AutoIT, the only problem with it is that I don't undertsand how to use it with seleium
from selenium import webdriver
import autoit
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://na58.evony.com/s.html?loginid=747970653D74727947616D65&adv=index")
driver.maximize_window()
assert "Evony - Free forever " in driver.title
So far I have this and It's doing what is suppose to do which is create a new account using that "driver.get" but when I reach to the page, it is all flash and I can not interact with anything in the webpage so I have to use AutoIT but I don't know how to get it to "pick-up" from where selenium left off. I want it to interact with a button on the webpage and from viewing a previous post on stackoverflow I can use a (x,y) to specify the location but unfortunately that post didn't explain beyond that. Any and all information would be great, thanks.
Yes, you can use any number of scraping libraries (scrapy and beautiful soup are both easy to use and very powerful). Personally though, I like Selenium and its python bindings because they're the most flexible. Your final script would look something like this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://xx.yy.zz")
# Click the "New comer, click here to play!" button
elem = driver. find_element_by_partial_link_text("Click here to play")
elem.send_keys(Keys.RETURN)
Can you post what the source of the page looks like (maybe using a Pastebin)?
Edit: updated to show you how to click the "Click here to play" button.

searching web-client for python that can handle frames

I'm searching for a python module that simulates a webbrowser and can handle html frames. I want to use the chatbot Brain http://www.thebot.de/ with python. If you know any tutorials that explain how to use your suggested module in relation with forms and frames give me a links to them please.
Tim,
I suggest you take a look at Selenium. By default it opens and manipulates Firefox to navigate the web. Its primary use case is testing, however in a pinch I've used it in some of my scripts to get past sites with a lot of javascript or, as in this case, iframes.
The basic usage is:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
driver.close()
To access an iframe you can use the following code:
elm = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame(elm)
Then when you want to switch out of the iframe:
driver.switch_to.default_content()
You can even utilize Selenium headless with xvfbwrapper like this (code from github.com/cgolberg/xvfbwrapper):
from xvfbwrapper import Xvfb
vdisplay = Xvfb()
vdisplay.start()
# launch stuff inside virtual display here
vdisplay.stop()

Python/Selenium - Chrome Web Driver, Click Action

I'm having an issue coding a click action using Selenium and the Chrome Web Driver in Python. I've spent some time googling around and found that I have to use another selenium process in order to make a click in Google Chrome, which doesn't make sense to me (Wouldn't it be something while calling webdrive.Chrome?). Though I can't find any other method to make a click, either online or by going through seleniums modules.
Here's what I have, any help is appreciated! Thanks!
EDIT: So I found the ActionChains module in Selenium, can't seem to get this to work either. Updated my code, a bit, still stuck. Does the ChromeDriver really just not support clicks?
import selenium
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
chromeOps = webdriver.ChromeOptions()
chromeOps._binary_location = "C:\\Applications\\Browser\\Chrome.exe"
chromeOps._arguments = ["--enable-internal-flash"]
browser = webdriver.Chrome("C:\\Applications\\Browser\\chromedriver.exe", port=4445, chrome_options=chromeOps)
time.sleep(3)
browser.get("http://example.com")
##selenium.selenium("127.0.0.1", 4445,'*Chrome.exe', 'https://example.com').click("//a[contains(#href,'http://example.com/link')]")
webdriver.ActionChains(browser).click(on_element='//a[contains(#href,"http://example.com/link")]')
I hate it when such simple things are right infront of you.
clickme = browser.find_element_by_xpath('//a[contains(#href,"http://example.com/link")]')
clickme.click()

Categories

Resources