I have written the following code to login to a website. So far it simply gets the webpage, accepts cookies, but when I try to login by clicking the login button, the page hangs and the login page never loads.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, ElementNotInteractableException
# Accept consent cookies
def accept_cookies(browser):
try:
browser.find_element_by_xpath('//*[@id="gdpr-banner-accept"]').click()
except NoSuchElementException:
print('Cookies already accepted')
# Webpage parameters
base_site = "https://www.ebay-kleinanzeigen.de/"
# Setup remote control browser
fireFoxOptions = webdriver.FirefoxOptions()
#fireFoxOptions.add_argument("--headless")
browser = webdriver.Firefox(executable_path = '/home/Webdriver/bin/geckodriver',firefox_options=fireFoxOptions)
browser.get(base_site)
accept_cookies(browser)
# Click login pop-up
browser.find_elements_by_xpath("//*[contains(text(), 'Einloggen')]")[1].click()
Note: There are two login buttons (one popup & one in the page), I’ve tried both with the same result.
I have done similar with other websites, no problem. So am curious as to why it doesn’t work here.
Any thoughts on why this might be? Or how to get around this?
2
Answers
I modified your code a bit adding a couple of optional arguments and on execution I got the following result:
Code Block:
Observation: My observation was similar to your’s that the page hangs and the login page never loads as shown below:
Deep Dive
While inspecting the DOM Tree of the webpage you will find that some of the
<script>
and<link>
tag refers to JavaScripts having keyword dist. As an example:<script type="text/javascript" async="" src="/static/js/lib/node_modules/@ebayk/prebid/dist/prebid.10o55zon5xxyi.js"></script>
window.BelenConf.prebidFileSrc = '/static/js/lib/node_modules/@ebayk/prebid/dist/prebid.10o55zon5xxyi.js';
This is a clear indication that the website is protected by Bot Management service provider Distil Networks and the navigation by ChromeDriver gets detected and subsequently blocked.
Distil
As per the article There Really Is Something About Distil.it…:
Further,
Reference
You can find a couple of detailed discussion in: