我有一个 Python 应用程序,它在 OSX 上与 selenium 配合得很好,我已经在 Docker 中安装了这个应用程序(Debian 容器上的 Python 映像)。当它在 OSX 上运行良好时,但在 debian 上却不是相同的结果。
这个 Python 脚本的目的是点击 React 按钮并解析 HTTP POST 请求。
这个按钮的 Xpath 是
//*[@id='container']/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button
这是此网址的“Rechercher”按钮: https ://www.leboncoin.fr/annonces/offres/lorraine/
def headless(url):
url_lbc = 'https://api.leboncoin.fr/finder/search'
for x in range(0, 10):
http_tunnel_port, tor_process, socks_port = tor_sub()
server = browsermob_server()
proxy_b = browsermob_proxy(server, http_tunnel_port)
profile = profile_firefox(proxy_b)
options = options_firefox()
print('Initialisation du Test N° : '+ str(x))
driver = webdriver.Firefox(executable_path='browserup/tools/geckodriverLinux', firefox_profile=profile, options=options)
proxy_b.new_har("file_test", options={'captureHeaders': True, 'captureContent': True})
driver.set_window_position(0, 0)
driver.set_window_size(randint(1024, 2060), randint(1024, 4100))
time.sleep(randint(6,10))
driver.get(url)
time.sleep(randint(2, 3))
try:
if driver.title == 'You have been blocked':
driver.save_screenshot("filename"+str(x)+".png")
print("Ban")
firefox_closing(driver, server)
else:
driver.save_screenshot("filename"+str(x)+".png")
driver.find_element_by_xpath("//*[@id='container']/main/div/div/div[1]/div[2]/form/div/div[2]/div[1]/button").click()
print("Connecté a LBC")
for ent in proxy_b.har['log']['entries']:
for request in ent:
if ent['request']['url'] == url_lbc and ent['request']['method'] == 'POST':
header_raw = (ent['request']['headers'])
payload = (ent['request']['postData']['text'])
header = {value['name']: value['value'] for value in header_raw}
print('Test N° : '+ str(x) + " ====> OK ")
return header, payload
time.sleep(1)
在 OSX 8/10 上尝试是成功的,在 Debian 上只有 2/10,我改进此脚本的第一个选项是找到等待反应按钮的最佳选项,并在加载 DOM 或按钮时单击它。非常感谢你的帮助。