Site recognizing Python Selenium Webdriver on multiple pages - python

I am interested in using Selenium with Python to allow multiple bots to play poker against themselves on Pokernow (https://www.pokernow.club). You can create your own poker game and share a link for others to join. I have written a bot using Selenium that creates a game (and is player 1) and instantiated a new webdriver (with the shareable link) for a second bot to join the game. If I use the same webdriver browser (Chrome), however, the site recognizes that the p2 request is coming from the same source as p1 and assumes that p2 is p1. This behavior also occurs if done manually using the same browser, even using incognito mode.
This can be fixed by instantiating the second webdriver with Safari, however I am curious if there is a more elegant solution to allow both webdrivers to use Chrome without the site recognizing that they are requesting from the same source. I would like to have more than two players and I am running out of additional browsers to use.

Probably recognizes using cookies. You can try to use new instance of webdriver for each player. Every instance uses new profile, and this should make browsers independent:
driver1 = webdriver.Chrome() # for player 1
driver2 = webdriver.Chrome() # for player 2
You can also use Selenium Hub with Docker and completely separate or use different browsers.

Related

How to play a Youtube video in python

What modules should I use to make a Python script where Python gets the name of a video, opens up a Chrome window, opens Youtube and plays the first result. I've tried Youtube Data API but it's not for that kind of things.
You can use the subprocess to call a browser and run the url you request. With this minimal code you can ask for a website to go, and open in firefox. But you can use it as a bootstrap code for your application using chrome.
import subprocess
url = input('What site you would to go: ')
subprocess.Popen(['firefox',url])
Using this template, you can just change the code to ask for a youtube link. If you have firefox installed you can just run it.
from selenium import webdriver
# You could also use Chrome, I just default to Firefox
driver = webdriver.Firefox()
driver.get('URL within some quotes')
# And if you want full-screen, finish it off with
driver.maximize_window()
And boom, that's a really quick way to open a site. You need to pip install selenium, of course, but this is my preferred way. Just wanted to offer an additional option.

Python - Manipulate and read browser from current browser

I am struggling to find a method in python which allows you to read data in a currently used web browser. Effectively, I am trying to download a massive dataframe of data on a locally controlled company webpage and implement it into a dataframe. The issue is that the website has a fairly complex authentication token process which I have not been able to bypass using Selenium using a slew of webdrivers, Requests, urllib, and cookielib using a variety of user parameters. I have given up on this front entirely as I am almost positive that there is more to the authentication process than can be achieved easily with these libraries.
However, I did manage to bypass the required tokenization process when I quickly tested opening a new tab in a current browser which was already logged in using WebBrowser. Classically, WebBrowser does not offer a read function meaning that even though the page can be opened the data on the page cannot be read into a pandas dataframe. This got me thinking I could use Win32com, open a browser, login, then run the rest of the script, but again, there is no general read ability of the dispatch for internet explorer meaning I can't send the information I want to pandas. I'm stumped. Any ideas?
I could acquire the necessary authentication token scripts, but I am sure that it would take a week or two before anything would happen on that front. I would obviously prefer to get something in the mean time while I wait for the actual auth scripts from the company.
Update: I received authentication tokens from the company, however it requires using a python package on another server I do not have access too, mostly because its an oddity that I am using Python in my department. Thus the above still applies - need a method for reading and manipulating an open browser.
Step-by-step
1) Start browser with Selenium.
2) Script should start waiting for certain element that inform you that you got required page and logged in.
3) You can use this new browser window to login to page manually.
4) Script detects that you are on required page and logged in.
5) Script processes page the way you like.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# start webdriver (opens Chrome in new window)
chrome = webdriver.Chrome()
# initialize waiter with maximum 300 seconds to wait.
waiter = WebDriverWait(chrome , 300)
# Will wait for appear of #logout element.
# I assume it shows that you are logged in.
wait.until(EC.presence_of_element_located(By.ID, "logout"))
# Extract data etc.
It might be easier if you use your Chrome user's profile. This way you may have previous session continued so you will not need to do any login actions.
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=FULL_PATH__TO_PROFILE")
chrome = webdriver.Chrome(chrome_options=options)
chrome.get("https://your_page_here")

How does Chrome Driver work, but Firefox, PhantomJS and HTMLUnit not?

I am trying to scrape a dynamic content (javascript) page with Python + Selenium + BS4 and the page blocks my requests at random (the soft might be: F5 AMS).
I managed to bypass this thing by changing the user-agent for each of the browsers I have specified. The thing is, only the Chrome driver can pass over the rejection. Same code, adjusted for PhantomJS or Firefox drivers is blocked constantly, like I am not even changing the user agent.
I must say that I am also multithreading, that meaning, starting 4 browsers at the same time.
Why does this happen? What does Chrome Webdriver have to offer that can pass over the firewall and the rest don't?
I really need to get the results because I want to change to Firefox, therefore, I want to make Firefox pass just as Chrome.
Two words: Browser Fingerprinting. It's a huge topic in it's own right and as Tarun mentioned would take a decent amount of research to nail this issue on its head. But possible I believe.

Python selenium with chrome webdriver - change user agent

I am running some code with selenium using python, and I figured out that I need to dynamically change the UserAgent after I already created the webdriver. Any advice if it is possible and how this could be done? Just to highlight - I want to change it on the fly, after almost each GET or POST request I send
I would go with creating a new driver and copy all the necessary attributes from the old driver except the user agent.

Selenium unique sessions

I want to use Selenium on a certain website. I have to open the website multiple times simultaneously with each instance having an unique session. I can open multiple windows but can't seem to create an unique session for each one.
I did some research and found Selenium Grid and some online platforms which can run all kinds of browsers to test your application but this seems to be more complicated than it has to be.
So how can I use Selenium to visit a website multiple times simultaneously with each one having an unique session?
Kind Regards,
Martin
I once answered a similar question.
Selenium actually runs private mode by default. Every time you start any driver via Selenium it creates a brand new anonymous profile/session.
This means that you can instantiate multiple drivers, which will open multiple new windows and each of those should normally not be influenced by the others.
For instance the code:
from selenium import webdriver
import time
driver1 = webdriver.Firefox()
driver1.get("http://www.python.org")
driver2 = webdriver.Firefox()
driver2.get("http://www.python.org")
time.sleep( 20 )
will open 2 windows that are using different sessions. I used the sleep method, so you can see the windows visually.

Categories

Resources