【发布时间】: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
在预期的中间链接之前有零到三个中间链接,所以任何一个都没有出现。前两个以随机顺序出现:
-
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 网页,见下文。
-
http://regecish.net/etc..。 (这是 adfly 网站,一旦可用,您需要点击此处的跳过广告按钮)。
-
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