I am currently working on a webscraping project using Selenium in python.
My code works as intended when run from a web driver in non-headless mode. However, it is not the case when it is run in headless mode. For instance, if I try to extract text from a website, the non-headless mode returns the text, while the headless mode returns None. (I have included some code below for reference).
First, I constructed the webdriver with the following code (the opt.headless is set to True or False in order to switch between headless and non-headless)
def getHeadlessDriver():
opts = webdriver.ChromeOptions()
opts.headless = False
driver = webdriver.Chrome(ChromeDriverManager().install(), options=opts)
return driver
Then, I used the find_elements_by_xpath function to extract texts data from a website. A sample code is provided below:
driver = getHeadlessDriver()
feedbacks = driver.find_elements_by_xpath(
"//div[contains(@class, 'LiveFeedbackSectionViewController__LiveFeedbackStatusItem-sc-1ahetk9-4 cUJPkM')]")
for feedback in feedbacks:
print(feedback.text)
I did some googling to find explanation for why the headless mode does not work, but I am still not sure. From my understanding, a headless mode "acts the same", but just without a Graphical User Interface.
Could there be a problem with the implementation of my code? Or does headless mode have other differences other than not having a graphical user interface?
Thank you.