Selenium从服务器获取URL地址响应

时间:2019-05-13 08:06:24

标签: python selenium

我尝试使用python硒从'zumi'服务器获取URL地址响应。

例如: 'https://www.zumi.pl/2421025,Marcin_Piatek_Pimar,Warszawa,firma.html#homePage'

当我在浏览器中键入以下内容时:

'https://www.zumi.pl/2421025,a,a,firma.html'

我获得了以上的回应。

现在,我尝试使用python硒获得此响应:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome(chrome_driver, options=chrome_options)
url = 'https://www.zumi.pl/2421025,a,a,firma.html'
browser.get(url)

current_url = browser.current_url

如何处理此问题以获得正确的响应,例如: 'https://www.zumi.pl/2421025,Marcin_Piatek_Pimar,Warszawa,firma.html#homePage'

2 个答案:

答案 0 :(得分:0)

我借助以下方法找到解决方案:

https://tarunlalwani.com/post/reusing-existing-browser-session-selenium/

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
browser = webdriver.Chrome(chrome_driver, options=chrome_options)
url = 'https://www.zumi.pl/Drukarnie+Bartk%C3%B3w,namapie.html'
browser.get(url)

# get browser session parameters
executor_url = browser.command_executor._url
session_id = browser.session_id

# create remote second browser with session data from first one
browser_session = webdriver.Remote(command_executor=executor_url, desired_capabilities=desired_capabilities)
browser_session.session_id = session_id

# url for response
url2 = 'https://www.zumi.pl/3108553,a,a,firma.html'

# get response from url2
browser_session.get(url2)
proper_url = browser_session.current_url

答案 1 :(得分:0)

  

这应该可以解决您的问题,而无需创建第二个浏览器会话:

from selenium import webdriver
import time
import traceback

browser = webdriver.Chrome("/usr/bin/chromedriver")


url = 'https://www.zumi.pl/2421025,a,a,firma.html'
browser.get(url)
time.sleep(4)

try:

    button = browser.find_elements_by_css_selector('.cmp-closebutton_hasBorder >svg > path')
    button[0].click()

except Exception as e:
    print(e)
    print(traceback.print_exc())

cmp-closebutton_hasBorder 是模型标头关闭跨度标签类

我在Ubuntu-18.04和chrome-browser-74.0上进行了测试