使用 webdriver 和 Selenium 访问网站被拒绝

Access denied to website using webdriver with Selenium

即使是使用“webdriver Chrome”打开的网站也有问题。只是试图打开以“访问被拒绝”信息结尾的网站,不知道为什么。 下面是我的代码:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

import time

class PriceCheckPhoenix:
    def __init__(self):
        self.url_login = "https://www.phoenixcontact.com/online/portal/pl?1dmy&urile=wcm%3apath%3a/plpl/web/home"

        self.create_session()

    def create_session(self):
    # Run browser with webdriver

        driver = webdriver.Chrome(executable_path="D:/chromedriver_v84.exe")
        driver.get(self.url_login)

        time.sleep(2)

        # Find link to sub-website with login
        link = driver.find_element_by_xpath('//*[@id="pxc-funcnav"]/div[3]/ul/li[1]/a').get_attribute("href")
    
        driver.get(link)

        time.sleep(100)

代码说明:

#1 我创建浏览器 chrome 会话

#2 从 self.url_login

加载第一个网站

#3 已加载

#4 需要在网站活动文字后面找一个link才能登录

#5 我找到它并尝试打开它,但是得到 link 后的响应是:

Access Denied
You don't have permission to access 
"http://www.phoenixcontact.com/online/portal/pl/pxc/offcontext/login/!ut/p/z1/tZJNa4NAEIZ_Sw45yszuuro9WkO1xqY2EqN7EbXGWPzYFDGlv74Gcio0oYTMZRgY3mcYHpAQg-yysa6yoe67rJnmRBqpu4zownzixDEYx2cWmIYTeYgrHSKQIFVRv0MieJZTZEITglFNLwTXRPaw03RGC6Qm10nOTttFN6hhD4lqVDPHY5nPcd-3JSQTy0ypQ5C4Onl5XUcmvgXCttzNWo-WCNuxLo-w6frPdjot_CfZxWsEciPhSjy7a7xN7xt_63M8kJdNmlSrPw4HaU2G9N1Qfg0Q_1Zke4JeiPHIeQH_KAshVE0a-GkQ24EPqm0F41WbLh5XWuKN3-fm78KgsmazH7dw0Ts!/dz/d5/L0lJSklKQ2dwUkEhIS9JRGpBQUF4QUFFUkNwcVlxLzRObEdRb1lwTWhUalVFZyEvWjZfR0FMNjE0ODI4RzNEQzBJMklPMlA2OTFHMDMvWjdfR0FMNjE0ODI4RzNEQzBJMklPMlA2OTFHSTcvdGFyZ2V0Vmlldy9sb2dpbg!!/" on this server.
Reference #18.d58655f.1597921471.5b29112

有人知道这里出了什么问题吗? :( 当我尝试在普通 Chrome 浏览器中从 link 加载网站时,一切正常:/ 谢谢大家的帮助。

请尝试下面的代码,让我知道它是否适合你:-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time

options = Options()
user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36'
options.add_argument('user-agent={0}'.format(user_agent))

driver = webdriver.Chrome(options=options)
wait = WebDriverWait(driver, 20)
action = ActionChains(driver)

driver.get("https://www.phoenixcontact.com/online/portal/pl?1dmy&urile=wcm%3apath%3a/plpl/web/home")
Login_Btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//*[@class='pxc-fn-login']/a")))

action.move_to_element(Login_Btn).click().perform()

注意 - 请相应地更改您的代码。