2

I am trying to get automatically the result of the URL shorten. This is the page what I am using: url shortener site

This is the code I made (URLS list contains links):

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)

After this I get the shortened url. I would need a little help to get it somehow.

Guy
  • 46,488
  • 10
  • 44
  • 88

3 Answers3

0

Use WebDriverWait to wait for short url result and get the value.

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)
Sers
  • 12,047
  • 2
  • 12
  • 31
0

You need use page.source, because it translate you page on need code, for chromedriver, some like lifehuck )) or you can use get_attribute('innerHTML') - you can access everything from the page.

Python WebDriver how to print whole page source (html)

Vitalii
  • 113
  • 1
  • 1
  • 11
0

To extract the result value of the URL shortener automatically you need to induce WebDriverWait for the visibility_of_element_located() and you can use the following Locator Strategies:

  • Code Block:

    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:\WebDrivers\chromedriver.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()
    
  • Console Output:

    http://paylinx.pw/linx/Uksheqw8
    http://paylinx.pw/linx/s0DA44C
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352