Search code examples
pythonseleniumweb-scrapingselenium-chromedriveruser-agent

how to remove square brackets from user agent header?


The following code changes the user-agent:

        
        headers = randint(0, 1)
        with open('headers.csv', 'r') as fd:
            reader = csv.reader(fd)
            reader = list(reader)
            driver.execute_cdp_cmd('Network.setUserAgentOverride', {
                                    "userAgent": str(reader[headers])})    
        
       

The headers csv file contains only two headers for testing purposes: headers.csv:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36

When going to 'https://amiunique.org/fp' on my normal browser, the user agent header is normal. normal browser

And of when using selenium chrome driver using the above user-agent changing code, this is the result: selenium chromedriver

I am assuming that the [] parenthesizing the user-agent header is what is causing me to be detected. how do I get rid of the [] from the output?


Solution

  • Your item (reader[headers]) is a list itself. Use

    "userAgent": ''.join(reader[headers])
    

    instead.