简体   繁体   English

如何获得与 Selenium Python 的链接?

[英]How to get link with Selenium Python?

I am trying to get the link under a button ('Click Here To Play') on a webpage:我试图在网页上的按钮(“单击此处播放”)下获取链接:

在此处输入图片说明

This is the page inspect in Chrome.这是 Chrome 中的页面检查。

在此处输入图片说明

I am trying to get the highlighted "href" from <a href="https://vidtodo.com/.... This is my code:我试图从 <a href="https://vidtodo.com/.... 这是我的代码:

from selenium import webdriver
url = 'https://www1.swatchseries.to/freecale.html?r=qepyJmjdCI6Ilo2SWFPdlk51dDBoMSmtxeHFnWXBbVOEJrcVdVMXhPT2t0bUZEZzNtNEd5ZVhCNHYrWnZkT0NZYzdZaWpmZlB0alEiLCJpdiI6IjFmYTdhMzZjYjJhODc1ZmIxODQ4MzVhZDc2N2MyYjNiIiwicyI6Ijc2NTZiMDg0MDFhNmQ1NjYifQ=='
driver = webdriver.Chrome('drivers/chromedriver.exe')
driver.maximize_window()
driver.get(url)
elements = driver.find_elements_by_xpath('/html/body/div[2]/div[2]/div/div[2]/div/div/div/div/div/div/div/div[2]/a')
for element in elements:
    print(element.get_attribute('href'))

However, I am getting this as result, rather than the link I want:但是,我得到了这个结果,而不是我想要的链接:

http://www1.swatchseries.to/

Process finished with exit code 0

Any suggestions on how to get the link I need?关于如何获取我需要的链接的任何建议?

To get link from the Click Here To Play button you need to要从Click Here To Play按钮获取链接,您需要

Induce WebDriverWait () and wait for visibility_of_element_located () and you can use any of the following locator. Induce WebDriverWait () 并等待visibility_of_element_located () ,您可以使用以下任何定位器。

XPATH :路径

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//a[text()='Click Here to Play']"))).get_attribute("href"))

Css Selector: CSS 选择器:

print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,"a.push_button.blue"))).get_attribute("href"))

Import below libraries.导入以下库。

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

It seems that a script is dynamically changing the link target (the script is right above the link).似乎脚本正在动态更改链接目标(脚本位于链接正上方)。 It is likely that you're getting the link too early and this shows the placeholder you get.您可能过早地获得链接,这显示了您获得的占位符。

Try waiting a few seconds, eg time.sleep(5) and see if that changes the link URL.尝试等待几秒钟,例如time.sleep(5)并查看是否会更改链接 URL。 If that is the case, a proper solution would be a while loop checking against the placeholder link, and waiting for it to change to the actual URL.如果是这种情况,正确的解决方案是使用while循环检查占位符链接,并等待它更改为实际 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM