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

  • \$\begingroup\$ Welcome to Code Review! Could you share a usage example to see how you put it all together? More context is usually better at Code Review. \$\endgroup\$ – Mast Apr 10 '18 at 4:50
  • \$\begingroup\$ @Mast What I have done here is, I got the full html file from the above-mentioned site and I scraped the required data which is the username and then I made a while loop that keeps on clicking the element new button until the word 'female' in username evaluates to true. \$\endgroup\$ – Daniel Sam Apr 10 '18 at 6:17

Your third part is wrong, I think.

nonCompatible = 'female' not in name

will only run against the value of name at the time of execution, so nonCompatible will end up either True or False, and won't change unless you reset it elsewhere in the while loop. You may want to either inline it:

while 'female' not in name:

or abstract it:

def nonCompatible(nym):
    return 'female' not in nym

while nonCompatible(name):
  • \$\begingroup\$ Thanks for finding the problem. However, I still can't solve even by inlining 'female' not in name in the while loop. The problem is that using selenium webdriver I'm able to get the full html source of the first user only then onwards even if I'm putting it in a while loop the html file remains the same hence the username is also same. \$\endgroup\$ – Daniel Sam Apr 10 '18 at 6:44
  • 1
    \$\begingroup\$ You've only given code fragments, so it's difficult to tell exactly what's wrong. Also: SO is for fixing problems, this place is for reviewing how well it was fixed. \$\endgroup\$ – pjz Apr 10 '18 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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