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'

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:
    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.


1 Answer 1


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
  • \$\begingroup\$ Thank you for all the fixes but actually, I'm not looking for all the usernames as the objective of this program is to match me to a female user, and I think you have visited the chatrooms but I intend on using this for their anonymous chat service where only a username is provided. \$\endgroup\$
    – Daniel Sam
    Commented May 22, 2018 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.