Program to automate skipping users on a chat website until the username meets certain conditions (v.2.0) This is a python program that automates skipping through users on a chat website called talkwithstrangers using the Selenium Webdriver and BeautifulSoup modules of Python.

The first part uses Selenium to open the browser:

from selenium import webdriver
driver = webdriver.Chrome('C:\SeleniumDrivers\chromedriver.exe')
userName = 'null'
driver.get('https://talkwithstranger.com/talk-to-strangers')


The second part is a while loop that runs while the username has not met the required conditions:

while 'female' not in userName or 'Female' not in userName:
    driver.get('https://talkwithstranger.com/talk-to-strangers')
    html = driver.execute_script("return document.documentElement.innerHTML;")

    from bs4 import BeautifulSoup
    soup = BeautifulSoup(html, 'html5lib')
    userVal = soup.find(class_='alert text-center alert-success')
    userNames =  userVal.find_all('b')

    def getName(userNames):
        for name in userNames:
            return name.prettify()

    userName = getName(userNames)


The code is running fine but the problem is the efficiency and runtime.
There are instructions you should not repeat inside the loop, and they should be written before it:


from bs4 import BeautifulSoup should be written much above in your code, within the import section.
getName() should be defined outside the loop, and named get_names() according to the naming conventions (actually it should be named get_names() because you are dealing with more than one name)


You do not get all female users:


When you run userName = getName(userNames), you are getting the last female user whereas you are obviously looking for all of them. So you should create a list of usernames and append useName (should be written user_name) to it whenever found.


Main performance issue:


You quickly refresh the page using driver.get('https://talkwithstranger.com/talk-to-strangers') whereas when I visited the link, it does not refresh as frequently as you do it. In opposite, most often I am kicked out of the room and had to wait a dozen of seconds. I think it also depends on whether there are users or not. In all cases, you are refreshing the page too quickly and you have to study the behavior of the website to decide on the strategy and criteria upon which you can refresh the page