I have a Selenium bot that interacts with EA Sports FIFA 21 web app, where you can buy and sells in-game football players. The interface looks like this.
Lets say my original program was a single script that:
login() - logged into the platform
goToTransferMarket() - navigated to the ‘market search’ page
inputSearchParameters(playername) - inputted my search parameters to search
clickSearch() - clicked Search button
evaluateResults(buy_price) - evaluated results and bought players cheaper than buy price
If any method failed, I’d have to quit and restart from scratch. To fix this, I built a tkinter GUI with buttons for each function that executes in their own thread. This way If a function fails, like clicking the search button, I can manually perform the action and proceed to test the subsequent methods.
But still, to fix the broken methods, I have to restart the program from scratch which requires a tedious login process to fix something as simple as forgetting to cast a string to an integer. Logging in a lot also draws unwanted attention.
Given that I’m already logged in during the current testing session, is there a way to recompile my program and ‘grab’ the existing webdriver session?
Even better, is it possible to dynamically change my functions and update them with an ‘update with latest code’ button in my GUI?
The 'player list' box in my GUI writes to a textfile, which allows me to change who the bot is searching for in real time. The textfile is passed to the main bot search method when I click the "Bid using list" button. This is what made me wonder if I can set it up to somehow recompile parts of my code in a separate thread and keep my existing webdriver session. Any help would be amazing thank you.
You can add a infinite while loop and try catch to make sure your test never fails else :
If you are using chrome :
you can connect to existing chrome session using debuuger address, so start the chrome with specified debugging port from cmd
Start chrome with debug port: (In windows , search for other os how to start chrome in a specific debug port)
<path>\chrome.exe" --remote-debugging-port=1559
And in selenium use :
JAVA:
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress", "127.0.0.1:1559");
WebDriver browser=new ChromeDriver(options);`
Python :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:1559")
driver = webdriver.Chrome(options=chrome_options)
Related
I would like to create a new session for each tab I open and then control the sessions individually using Selenium python. Is this possible?
#reynoldsnp: Firefox has an official addon that does this, but I'm not sure if you can get selenium to interact with the addon. addons.mozilla.org/en-GB/firefox/addon/multi-account-containers If you figure out a way to do it, I would love to know how.
(I can't comment yet due to my reputation score, therefore quoted comment).
I don't know how to actually interact with the extension but if you have a known set of sites you would like to open:
Try this:
Make a firefox profile for your use with selenium. Multiple profiles
Windows 8/8.1/10:
Press Win + R on your keyboard.
Type firefox --new-instance --ProfileManager
Open Firefox in that profile by selecting the new profile in the setup wizard. Install the extension in that profile.
Set up the containers you would like in the extension, to, by default, open up with a specific site.
Ensure that the checkbox is ticked.
Start selenium with that profile like this:
from selenium import webdriver
profile = webdriver.FirefoxProfile('path/to/your/profile') # on windows found here: %APPDATA%/Mozilla/Firefox/Profiles
driver = webdriver.Firefox(firefox_profile=profile)
Navigate between tabs, effectively containers, using selenuium.
First, no, you cannot. While tabs runs as a process, they are attached to the session ID which initially open the browser. This is how the protocol works https://www.w3.org/TR/webdriver/#new-session
They have, however, a unique ID which you can use to identify them by and switch between them.
driver.window_handles
will give you the list of open tabs. Each tab is fully isolated. You can now choose between
driver.switch_to_window("any open tab taken from windows handles list")
driver.do_something
driver.switch_to_window("any other tab from windows handles list")
driver.do_something_else_on_other_tab
# or (this option can let you run in parallel)
driver a = ChromeDriver()
driver b = ChromeDriver()
a.do_something
b.do_something
As suggested (and I personally do myself) open new session for each tab you want, that way you can parallel them and run much faster, all in all.
I am not sure the performance difference is that significant between multiple browsers or multiple tabs... they should use almost the same resources.
I want to be able to use pure selenium webdriver to open a zoom link in Chrome and then redirect me to the zoom.us application.
When I execute this:
from selenium import webdriver
def main():
driver = webdriver.Chrome()
driver.get("https://zoom.us/j/000-000-000")
main()
I receive a pop-up saying
https://zoom.us wants to open this application.
and I must press a button titled open zoom.us to open the app.
Is there a way to press this pop-up button through selenium. Or, is there some other way to open zoom from chromedriver?
NOTE: I only want to use selenium. I have been able to implement pyautogui to click on the button but that is not what I am looking for.
Solution for Java:
driver.switchTo().alert().accept();
Solution for Python:
driver.switch_to.alert.accept()
There are a lot of duplicated questions regarding this issue. Here is one of them, and it is quite sure that selenium is not capable of achieving such job since it only interacts with the chrome page. I previously encountered this issue as well and here is my solution to it. It might look really unprofessional, but fortunately it works.
The logic of my solution is to change the setting of chrome in order to skip the popup and directly open the application you want. However, the Chrome team has removed this feature in the latter version for some reasons, and we need to get it back manually. Then, we know that everytime when selenium starts to do the thing it opens a new Chrome page with NO customized settings just like the incognito page. Therefore we need to do something to let selenium opened a Chrome page with your customized setting, so that we can make sure that the popup, which we changed manually to skip, can be skipped successfully.
Type the following code in your terminal.
defaults write com.google.Chrome ExternalProtocolDialogShowAlwaysOpenCheckbox -bool true
This enables you to change the setting of skipping popups, which is the feature Chrome team removed.
Restart Chrome,and open the zoom (or whatever application) page to let the popup display. If you do the 1st step correctly you will be able to see there is a checkbox shown next to the "Open Zoom.us" saying if you check it chrome will open this application without asking, that is, to skip the popup for this application.
Now we need to let selenium open the Chrome with our customized setting. To do this, type "chrome://version" in the search tab of your ordinary Chrome (Not automated page opened by selenium). Go to "Profile Path", and copy this path without the last word "default". For example:
/Users/MYNAME/Library/Application Support/Google/Chrome/Default
This is my profile path, but I only copy everything except the last word Default, so this is what I need to copy.
/Users/MYNAME/Library/Application Support/Google/Chrome/
This is for Mac users, but for Windows only the path is different(starts with C:// or something), steps are same.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
option = Options()
option.add_argument('THE PATH YOU JUST COPIED')
driver = webdriver.Chrome(executable_path='YOUR PATH TO CHROMEDRIVER', options=option)
driver.get("google.com") #Or anything else
We use "options" to let selenium open a page with our customized profile. Now you will see selenium opens a Chrome page with all your account profile, settings, and it just appears like your ordinary chrome page.
Run your code. But before that, remember to quit ALL CHROME sessions manually. For Mac, make sure that there is no dot under Chrome icon indicating that Chrome is not running for any circumstances. THIS STEP IS CRITICAL otherwise selenium will open a chrome page and it just stops there.
Here are all the steps. Again, this solution is vert informal and I personally don't think it is a "solution" to this problem. I will try to figure out a better way of achieving this in the future. But I still posted this as an alternative simply because I guess it might be helpful to some extent for somebody just like me. Hope it works for you, and good luck.
I’m trying to log in to a pretty complex (to my beginner’s eye) website and make a reservation. Did not know a single python statement before starting the project. After many starts and stops have successfully logged in using requests_html/HTMLSession. Have overcome the security/authorization issues and arrived at target page. The page displays the server time on it and I cannot hit the proper key until the time reaches 7:00 AM.
I am unable to access the field. I have tried the .search and .find commands, but nothing. I am hoping someone can tell me how to download the time into my program so I can test the time and wait until it reaches, or almost reaches 7:00. (I say almost because the reservation is for tee times and there is a real crunch at 7 – the whole point of this application is to automate the process and be the fastest!)
So I need to be able to load the time into my python and click a date file when the clock reaches 7:00.
No idea what scraping tool you are using, but generaly you would access this elemen via xpath or css selector:
response.css(".jquery_server_clock::text").extract()
This example is if you are using scrapy
Maybe you would be better off using selenium.
Selenium allows you to automate a browser window, so it's possible that it is not possible to interact with the site using requests, but using selenium the site you visit thinks you are using a normal browser but you can automate everything.
So what I would do if I were you:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("your_url.com")
input("Navigate to the desired page, then press enter")
while not driver.find_element_by_class_name("jquery_server_clock").text[0] == "7":
pass
driver.find_element_by_class_name("other_button").click()
This would wait until it is 7 AM and then click the other button immediately.
I'm trying to automate some form filling for a web app. Users have to login to the application and then start filling up pages of forms. I have the following Python script using Selenium that can open a window to my application:
from selenium import webdriver
driver = webdriver.Ie("C:\\Python\\Selenium\\Chrome\\chromedriver.exe")
driver.add_cookie()
driver.set_page_load_timeout(30)
driver.get("myurl/formpages")
driver.maximize_window()
driver.quit()
However, when Selenium starts the Chrome window, the user is not logged in. I want to bypass the need to log in every time.
On another Chrome window, I am already logged in as my test user. So whenever I go to the url on my Chrome window, I am already logged in and don't have to log in again.
Is there any way to pass this data into my Selenium script so that it uses the session currently on my existing Chrome instance, therefore not having to log in via the script?
Historically, this is not possible unfortunately (made frustrating by my agreement when I realize the effort it involves and for each browser!).
I've written code before that takes variables out of a CSV for username and password. This is bad because it's in plaintext but you can also hash the information if you like and handle that in your code.
So to recap, there are mediocre solutions, but no native way to handle this in selenium :(
Selenium by default creates a temporary chrome profile each time you start a new instance and deletes that profile once you quit the instance. If you change the chrome options in selenium driver to use a custom chrome profile and and allow that profile to save cookies, you will be able to login without each time typing your login details etc.
Hi i want to use selenium to open Internet Explorer-11 as different user. I have done a search and it show that it is possible but with Java, I am using Python so I am wondering is it possible with Internet Explorer webdriver or not.
For example if you right click on internet explorer, it will have the part sign in as different user. I want to automate that part with python IE webdriver, but I do not know how to do it.
what I am asking is similar to this post How to impersonate a specific user with Selenium Webdriver?
but I want to do it in Python with selenium webdriver
To be clear. I know how to open the webrowser with selenium and i know how to sign in with selenium (when there is a pop up window). But I am asking about how to let selenium know that I want to sign in as different user. Because if I just open my browswer normally there is no pop up for sign in.
from selenium import webdriver
driver=webdriver.Ie()
Thank you
You can use Selenium Web Driver.
Learning about it you can do what you need with a code similar to this:
username = selenium.find_element_by_id("username")
password = selenium.find_element_by_id("password")
username.send_keys("YourUsername")
password.send_keys("yourPa55worD")
selenium.find_element_by_name("submit").click()
Since it seems you want to use another OS user, I suggest you use Sikuli:
Sikuli automates anything you see on the screen. It uses image recognition to identify and control GUI components. It is useful when there is no easy access to a GUI's internal or source code.
I think there is no way to do what you are wanting with just Selenium. If you need to integrate Selenium and Sikuli, you can see this post on SOF: Calling to a Sikuli script from Python (Selenium) . It can give some ideas for you.
I hope it helps.
Download IE Drivers based on your OS (Windows 32 or 64 bit)
a. Download
Windows 32 bits driver
OR
b. Download Windows 64 bits driver
Extract the zip and copy IEDriverServer.exe file to some location e.g. E:\IEDriver
Write the following script
from selenium import webdriver
browser = webdriver.Ie("e:\\IEDriver\\IEDriverServer.exe")
send values your inputs 'username' and 'password' and submit.
Run the script, it should open IE browser...