我正在尝试自动获取URL缩短的结果。 这是我正在使用的页面:网址缩短器网站
这是我制作的代码(URL列表包含链接(:
driver.get("http://paylinx.pw/linx/")
for i in URLS:
driver.find_element_by_xpath('//*[@id="url"]').click()
time.sleep(2)
driver.find_element_by_xpath('//*[@id="url"]').send_keys(i)
time.sleep(2)
driver.find_element_by_xpath('//*[@id="invisibleCaptchaShort"]').click()
time.sleep(2)
在此之后,我得到了shortened url
.我需要一点帮助才能以某种方式获得它。
使用WebDriverWait
等待短 url 结果并获取值。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)
with driver:
driver.get("http://paylinx.pw/linx/")
for url in URLS:
driver.find_element_by_id("url").send_keys(url, Keys.ENTER)
short_url = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".add-link-result .input-lg"))).get_attribute("value")
print(short_url, url)
你需要使用page.source,因为它翻译你的页面需要代码,对于chromedriver,有些像lifehuck ((或者你可以使用get_attribute('innerHTML'( - 你可以从页面访问所有内容。
Python WebDriver 如何打印整个页面源代码 (html(
要自动提取 URL 缩短器的结果值,您需要诱导WebDriverWait等待visibility_of_element_located()
,您可以使用以下定位器策略:
-
代码块:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option('useAutomationExtension', False) URLS = ['https://selenium.dev/downloads/','https://selenium.dev/documentation/en/'] for i in URLS: driver = webdriver.Chrome(options=options, executable_path=r'C:WebDriverschromedriver.exe') driver.get("http://paylinx.pw/linx/") element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#url"))) element.clear() element.send_keys(i) driver.find_element_by_css_selector("button.btn-captcha#invisibleCaptchaShort").click() print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.input-group>input.form-control.input-lg"))).get_attribute("value")) driver.quit()
-
控制台输出:
http://paylinx.pw/linx/Uksheqw8 http://paylinx.pw/linx/s0DA44C