Using AutoIT with Selenium - python

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.

Related

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();
}

Scroll down section of webpage with selenium/python

I'm trying to scroll down a specific div in a webpage (ticker box in facebook) using selenium (with python).
I can find the element, but when i try to scroll it down using:
header = driver.find_element_by_class_name("tickerActivityStories")
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', header)
Nothing happens.
I've found a lot of answers, but mostly are for selenium in java, so i would like to know if it's possible to do it from python.
I've found a lot of answers, but mostly are for selenium in java
There is no difference, selenium has the same api in java and python, you just need to find the same function in python selenium.
First of all check if your JS works in the browser(if not, try scroll parent element).
Also you can try :
from selenium.webdriver.common.keys import Keys
header.send_keys(Keys.PAGE_DOWN)

Scraping a dynamic website via Python Scripts: how to get the values?

I am trying to scrape information from a website. So far, I've been able to access the webpage, log in with a username and password, and then print that landing page's page source into a separate .html/.txt file as needed.
Here's where the problems arise: on that "landing page," there's a table that I want to scrape the data from. If I were to manually right-click on any integer on that table, and select "inspect," I'd find the integer with no problem. However, when looking at the page source as a whole, I don't see the integers- just variable/parameter names. This leads me to believe it is a dynamic website.
How can I scrape the data?
I've been to hell and back trying to scrape this website, and so far, here's how the available technology has worked for me:
Firefox, IE, and Opera do not render the table. My guess is that this is a problem on the website's end. Only Chrome seems to work if I log in manually.
Selenium's Chromium package has been failing on me repeatedly (on my Windows 7 laptop) and I have even posted a question about the matter here. For now I'll assume it's just a lost cause, but I'm willing to graciously accept anyone's benevolent help.
Spynner's description looked promising, but that setup has frustrated me for quite some time- and the lack of a clear introduction only compounds its cumbersome nature to a novice like myself.
I prefer to code in Python, as it is the language I am most comfortable with. I have a pending company request to have the company install Visual Studio on my computer (to try doing this in C#), but I'm not holding my breath...
If my code can be of any use, so far, here's how I'm using mechanize:
# Headless Browsing Using PhantomJS and Selenium
#
# PhantomJS is installed in current directory
#
from selenium import webdriver
import time
browser = webdriver.PhantomJS()
browser.set_window_size(1120, 550) # need a fake browser size to fetch elements
def login_entry(username, password):
login_email = browser.find_element_by_id('UserName')
login_email.send_keys(username)
login_password = browser.find_element_by_id('Password')
login_password.send_keys(password)
submit_elem = browser.find_element_by_xpath("//button[contains(text(), 'Log in')]")
submit_elem.click()
browser.get("https://www.example.com")
login_entry('usr_name', 'pwd')
time.sleep(10)
test_output = open('phantomjs_test_source_output.html', 'w')
test_output.write(repr(browser.page_source))
test_output.close()
browser.quit()
p.s.- if anyone thinks I should be tagging javascript to this question, let me know. I personally don't know javascript but I'm sensing that it might be part of the problem/solution.
Try something like this. Sometimes with dynamic pages you need to wait for the data to load.
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(my_driver, my_time).until(EC.presence_of_all_elements_located(my_expected_element))
http://selenium-python.readthedocs.io/waits.html
https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

How to get CSS Background Colour with Python?

Basically exactly as the question says, I'm trying to get the background colour out from a website.
At the moment I'm using BeautifulSoup to get the HTML, but it's proving a difficult way of the getting the CSS. Any help would be great!
This is not something you can reliably solve with BeautifulSoup. You need a real browser.
The simplest option would be to use selenium browser automation tool:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('url')
element = driver.find_element_by_id('myid')
print(element.value_of_css_property('background-color'))
value_of_css_property() documentation.

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