单击登录按钮后,Ebay 网站挂起 - Selenium Python
Posted
技术标签:
【中文标题】单击登录按钮后,Ebay 网站挂起 - Selenium Python【英文标题】:Ebay website hangs after login button clicked - Selenium Python 【发布时间】:2021-04-15 21:18:44 【问题描述】:我编写了以下代码来登录网站。到目前为止,它只是获取网页,接受 cookie,但是当我尝试通过单击登录按钮登录时,页面挂起并且登录页面永远不会加载。
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()
注意:有两个登录按钮(一个弹出窗口和一个在页面中),我都尝试过,结果相同。
我在其他网站上也做过类似的,没问题。所以很好奇为什么它在这里不起作用。
对这可能的原因有什么想法吗?或者如何解决这个问题?
【问题讨论】:
【参考方案1】:我稍微修改了您的代码,添加了几个可选参数,并在执行时得到以下结果:
代码块:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get("https://www.ebay-kleinanzeigen.de/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='gdpr-banner-accept']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Einloggen')]"))).click()
观察:我的观察与您的相似,页面挂起并且登录页面永远不会加载,如下所示:
深入研究
在检查网页的DOM Tree 时,您会发现一些<script>
和<link>
标签引用了带有关键字dist 的javascripts。举个例子:
<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';
这清楚地表明该网站受到 Bot Management 服务提供商 Distil Networks 的保护,并且 ChromeDriver 的导航被检测到并随后被阻止强>。
蒸馏
根据文章There Really Is Something About Distil.it...:
Distil 通过观察网站行为和识别抓取工具特有的模式来保护网站免受自动内容抓取机器人的攻击。当 Distil 在一个站点上识别出恶意机器人时,它会创建一个列入黑名单的行为配置文件,并部署到其所有客户。类似于机器人防火墙的东西,Distil 检测模式并做出反应。
进一步,
"One pattern with Selenium was automating the theft of Web content"
,Distil 首席执行官 Rami Essaid 在上周接受采访时表示。"Even though they can create new bots, we figured out a way to identify Selenium the a tool they're using, so we're blocking Selenium no matter how many times they iterate on that bot. We're doing that now with Python and a lot of different technologies. Once we see a pattern emerge from one type of bot, then we work to reverse engineer the technology they use and identify it as malicious".
参考
您可以在以下位置找到一些详细的讨论:
Unable to use Selenium to automate Chase site login Webpage Is Detecting Selenium Webdriver with Chromedriver as a bot Is there a version of selenium webdriver that is not detectable【讨论】:
【参考方案2】:from selenium import webdriver
from selenium_stealth import stealth
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
# options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r"C:\Users\DIPRAJ\Programming\adclick_bot\chromedriver.exe")
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Intel Inc.",
renderer="Intel Iris OpenGL Engine",
fix_hairline=True,
)
url = "https://bot.sannysoft.com/"
driver.get(url)
time.sleep(5)
driver.quit()
【讨论】:
请为您的回答提供一些背景信息。它可以帮助解决问题的其他读者轻松理解您的方法。以上是关于单击登录按钮后,Ebay 网站挂起 - Selenium Python的主要内容,如果未能解决你的问题,请参考以下文章
swiftUI:登录完成后导航到主屏幕。通过按钮单击导航视图