【问题标题】:Python: store current page URL to variable in ChromePython:将当前页面 URL 存储到 Chrome 中的变量
【发布时间】:2021-06-18 04:55:51
【问题描述】:

我正在使用 Selenium,但这个特定页面阻止我简单地使用 "driver.current_url",因为它只是输出一个通用 URL 来掩盖真实的 URL。如果您想亲自尝试:https://render-state.to/crypt-of-the-damned/。点击 Google Drive 链接。

作为替代方案,我正在尝试使用keyboard inputs 复制Chrome 中当前页面的web address

from selenium import webdriver
import keyboard
import win32clipboard

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

START_PAGE = "https://github.com/boppreh/keyboard#keyboard.send"
driver.get(START_PAGE)

keyboard.send('alt+d') # select text in address bar
keyboard.send('ctrl+c') # should copy to clipboard, but does not.

win32clipboard.OpenClipboard()
data = win32clipboard.GetClipboardData()
win32clipboard.CloseClipboard()

print (data)

使用这些资源: https://github.com/mhammond/pywin32/releases https://github.com/boppreh/keyboard#keyboard.send

[][][][][][][][][][][][][][] 编辑 [][][][][][][][][] [][][][][]

要收集的预期链接是:https://drive.google.com/file/d/1cHPNOfcFh4BAwOBHUuxp3GmV-5a_BaTs/view

在预期的中间链接之前有零到三个中间链接,所以任何一个都没有出现。前两个以随机顺序出现:

  1. https://render-state.to/exit.php?redirect=https%3A%2F%2Fdrive.google.com%2Ffile%2Fd%2F1cHPNOfcFh4BAwOBHUuxp3GmV-5a_BaTs%2Fview%3Fusp%3Dsharing..。 (这是网站的 5 秒重定向,有时可以通过检查 Google Drive 按钮看到,但我还没有找到访问此 href 的方法,除非通过解析页面上的所有 href 并使用正则表达式查找模式,我如果所有其他方法都失败了,可能会尝试。如果有更简单的方法,则可以对其进行解析以产生最终预期的 Google Drive 链接。但是,有时它只是显示一个“http://ay.gy/etc”链接来加载无论如何都要注册 adfly 网页,见下文。

  2. http://regecish.net/etc..。 (这是 adfly 网站,一旦可用,您需要点击此处的跳过广告按钮)。

  3. https://iq-option.com/etc..。 (这有时会在您单击 adf.ly 跳过添加按钮后出现,并最终被返回而不是预期的链接)。

以下是针对每个重定向案例单独测试的代码。我还没有将它们与每个场景的条件连接起来。

*** 重定向 1:**

adfly = driver.find_element_by_link_text('GOOGLE DRIVE').get_attribute('href') # gives a "http://ay.gy/etc" link or a "https://render-state.to/exit.php?redirect=http%3A%2F%2Fq.gs%2FExBpX" link that can be parsed to "http://q.gs/etc". Both load the regecish adfly page, hence skipping redirect 1. 

*** Redirect 2**,等待并点击adfly网站上的skip add按钮:

driver.get(adfly)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="skip_bu2tton"]/img'))) 
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "mwButton")))
driver.find_element_by_xpath('//*[@id="skip_bu2tton"]/img').click()

*** Redirect 3**,当它出现时,加载一个随机的广告标签,例如 iq-option,然后立即创建最终的预期标签并激活它。

通过在已经专注于 GOOGLE 驱动器页面的同时尝试“driver.current_url”,无论隐式或显式等待如何,您最终都会得到三个重定向之一或原始起始页面。

【问题讨论】:

  • 基本上,当您点击 Google 驱动程序链接时,它会打开新标签页,您想要那个新标签页 URL 吗?
  • 为什么不直接从按钮获取链接。
  • @furas :这是有道理的。我试图检查href,它似乎在按钮上有所不同,当在新标签中打开时,这很奇怪
  • @cruisepandey 当我在网络浏览器中使用这个奇怪的链接(没有https://render-state.to/exit.php?redirect=)时,它会将我发送到最终将我重定向到 Google Drive 的页面 - 所以它必须是一些快捷链接。跨度>
  • 同时我测试了代码,我发现它使用了重定向——但是当我写我的第一个评论时你没有质疑它。如果您之前知道它,那么您应该有问题地写它 - 这样我们就不必为此浪费时间了。

标签: python selenium google-chrome selenium-chromedriver


【解决方案1】:

基本上,当您单击 Google 驱动程序链接时,它会打开新标签页,如果您想要该新标签页 URL,那么:

解释:当您点击 Google Drive 链接时,它会打开一个新标签。我们需要告诉司机它必须专注于新标签。现在的问题是我们将如何与司机沟通:

driver.switch_to.window(driver.window_handles[1]) 

使用这个命令。

代码:

driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("https://render-state.to/crypt-of-the-damned/")
print(driver.current_url)
ActionChains(driver).move_to_element(wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "GOOGLE DRIVE")))).click().perform()
driver.switch_to.window(driver.window_handles[1])
print(driver.current_url)

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

输出:

https://render-state.to/crypt-of-the-damned/
http://regecish.net/rweasy/-1VZSY/8138182/_eaHR0cHM6Ly9yZW5kZXItc3RhdGUudG8vZXhpdC5waHA/cmVkaXJlY3Q9aHR0cHMlM0ElMkYlMkZkcml2ZS5nb29nbGUuY29tJTJGZmlsZSUyRmQlMkYxY0hQTk9mY0ZoNEJBd09CSFV1eHAzR21WLTVhX0JhVHMlMkZ2aWV3JTNGdXNwJTNEc2hhcmluZw==?rndad=3058446250-1623997494

Process finished with exit code 0

【讨论】:

  • 结束网址是https://drive.google.com/file/d/1cHPNOfcFh4BAwOBHUuxp3GmV-5a_BaTs/view。我猜@SevenAltZero 想要获取这个 url。如果我没记错的话,那么添加 5 秒的等待时间就可以了
  • @LibinThomas :我仍然看不到那个 URL 在哪里。当我点击Google driver - 打开一个新标签并从那里检索当前网址
  • 分享重定向网址:render-state.to/…
  • 当您单击该页面上的谷歌驱动器链接时,该页面不同。 OP 非常具体地获取当前 URL
  • 点击GOOGLE DRIVE按钮后,我被重定向到谷歌驱动器页面,我认为这应该是理想的终点。因此,如果我们想获取重定向 url,那么将焦点切换到其他选项卡和 driver.current_url 应该可以工作。如果我们想要端点,添加 5 秒的等待,然后 driver.current_url 应该可以工作。让我知道这是否符合脚本的预期。
【解决方案2】:

将回答我自己措辞不当的问题,因为这可能对那些处理重定向的人有用。

通过重复获取所有打开的选项卡的 url,然后使用正则表达式在它们之间查找文件主机链接并解析它以产生所需的最终 URL,找到了解决方案。请注意,为了避免 Cloudflare,我还拦截了通过命令提示符而不是使用 driver.get() 启动的现有 chrome 页面。

测试页面:https://render-state.to/crypt-of-the-damned/

import re
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# wait precursors
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

print ('RUNNING')

# Intercept running chrome page after launching it via command prompt with:
# chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenium\ChromeProfile"
# This avoids Cloudflare.
# Start page = https://render-state.to/crypt-of-the-damned/

chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
print (driver.title)

# Grab adfly link from one the available hosts, in order of preference.
filehosts = ["MEDIAFIRE", "GOOGLE DRIVE", "UPLOADED", "RACATY", "WORKUPLOAD", "ZIPPYSHARE", "YANDEX", "UPLOADHAVEN", "FILES", "PASTEBIN", "GOFILE", "MEGA"]
ADFLY_Links = []            
move = True
xx = 0
while move == True:
    try:
        adfly = driver.find_element_by_link_text(filehosts[xx]).get_attribute('href')
        if adfly.find("render-state") > 0:
            adfly = adfly.replace('https://render-state.to/exit.php?redirect=http%3A%2F%2F','').replace('%2F','/')
        ADFLY_Links.append(adfly)
        print ('{} adfly links listed. {}.'.format(len(ADFLY_Links), filehosts[xx]))
        move = False
    except:
        move = True
        xx += 1

# Open adfly link.
driver.get(adfly)

# Wait adfly locker if it appears.
adfly_wait_lock = driver.current_url
if adfly_wait_lock.find("locked") > 0:
    driver.execute_script("window.history.go(-1)")
    print ('Waiting adfly lock...')
    time.sleep(20)
    driver.get(adfly)

# Wait for skip add button on regecish, click once available.
print ('Waiting Skip Ad button...')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="skip_bu2tton"]/img'))) 
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, "mwButton")))
driver.find_element_by_xpath('//*[@id="skip_bu2tton"]/img').click()

# Keep fetching links of all tabs and search them with regex until a filehost link appears.
wait_red = True
time_slept = 1
open_tabs = []
while wait_red:
    for handle in driver.window_handles: # Fetch URL of all tabs. Tried to fetch only relevant one, but had all sorts of annoyance.
        driver.switch_to.window(handle)
        open_tabs.append(driver.current_url)
        open_tabs_str = ''.join(open_tabs)
    to_find = re.compile("mediafire|drive|uploaded|racaty|workupload|zippyshare|yandex|uploadhaven|files|pastebin|gofile|mega")
    search_str = open_tabs_str
    match_obj = to_find.search(search_str)
    if match_obj == None:
        open_tabs = []
        open_tabs_str = ""
        time.sleep(1)
        print ('Waiting redirects... {} s'.format(time_slept))
        time_slept += 1
    else:
        wait_red = False

# Grab download link from fetched URLs.
print ('\n')
try:
    download_link = open_tabs[1] # for when Redirect 3 happens and you have 2 tabs open.
except:
    download_link = open_tabs[0] # For when it doesn't, and you only have 1.
if download_link.find("redirect") > 0: # Grab link from site's redirect if it becomes avaiblabe, avoiding 5 s wait.
    download_link = download_link.replace('https://render-state.to/exit.php?redirect=https%3A%2F%2F', '').replace('%2F', '/')
print (download_link)

最终代码实际上比这更复杂,因为它使用下面的代码来跳过 adfly 5 s 等待、处理错误等:

elems = driver.find_elements_by_tag_name("meta")
for elem in elems:
    content = elem.get_attribute("content")
    if content.find('redirecting') > 0:
        skip_adfly = content
        print ('Skipped Adfly')
        driver.get(skip_adfly)
        break

【讨论】:

    猜你喜欢
    • 2012-06-02
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2012-03-07
    • 2017-09-17
    • 2014-08-26
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多