Home > Mobile >  How can I make Selenium get a href with PYTHON?
How can I make Selenium get a href with PYTHON?

Time:08-16

I tried all the methods I found here https://selenium-python.readthedocs.io/locating-elements.html but I couldn't get that link from the page.

I need to get the link from the href

HTML page code:

<p style="margin: 0px 30px 30px 30px;text-align: center;-webkit-box-sizing: border-box;max-width: 100%;-moz-box-sizing: border-box;box-sizing: border-box;">
                                <!--[if mso]>
                                    <v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="https://u.pcloud.com/track?url=aHR0cHM6Ly91LnBjbG91ZC5jb20vPyNwYWdlPXZlcmlmeW1haWwmY29kZT1UTE9TN1pUWXN6MmxPSWh2bWhZUU9jd2RxSHk3bUJoejk3&token=j7yZTLOS7Z7Z87Zh354dzp5jNRuIf7aJshX1XzSehQX" style="height:49px;v-text-anchor:middle;width:289px;" arcsize="7%" strokecolor="#88CC17" fillcolor="#88CC17">
                                        <w:anchorlock/>
                                        <center style="color:#ffffff;font-family:sans-serif;font-size:13px;font-weight:bold;">CLICK TO VERIFY EMAIL</center>
                                    </v:roundrect>
                                <![endif]-->
                                <a href="https://u.pcloud.com/track?url=aHR0cHM6Ly91LnBjbG91ZC5jb20vPyNwYWdlPXZlcmlmeW1haWwmY29kZT1UTE9TN1pUWXN6MmxPSWh2bWhZUU9jd2RxSHk3bUJoejk3&token=j7yZTLOS7Z7Z87Zh354dzp5jNRuIf7aJshX1XzSehQX" style="color: #FFF;background-color: #88CC17;text-decoration: none;width: 285px;font-weight: 500;display: inline-block;padding: 13px 0px 13px 0px;border: 2px solid #88CC17;border-radius: 3px;-moz-border-radius: 3px;-webkit-border-radius: 3px;mso-hide:all;">
                                    CLICK TO VERIFY EMAIL
                                </a>

Some of the unsuccessful attempts:

link_ativador = navegador.find_element(By.LINK_TEXT, 'CLICK TO VERIFY EMAIL')
print(link_ativador)

link_ativador = navegador.find_element(By.XPATH, '/html/body/table/tbody/tr[2]/td/table/tbody/tr[3]/td/table/tbody/tr/td/p[3]/a').get_attribute('href')
print(link_ativador)

link_ativador = navegador.find_element(By.LINK_TEXT, 'CLICK TO VERIFY EMAIL').get_attribute('href')
print(link_ativador)

Some of the unsuccessful attempts:

link_try1 = WebDriverWait(navegador, 20).until(EC.presence_of_element_located((By.XPATH, '//a[text()="CLICK TO VERIFY EMAIL"]'))).get_attribute('href')
print(link_try1)

link_try2 = (WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, "CLICK TO VERIFY EMAIL"))).get_attribute("href"))
print(link_try2)

link_try3 = (WebDriverWait(navegador, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., 'CLICK TO VERIFY EMAIL')]"))).get_attribute("value"))
print(link_try3)

The full code:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

# Chrome e Proxy Tor
servico = Service(ChromeDriverManager().install())
proxy = "socks5://127.0.0.1:9150"  # Tor
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument(f"--proxy-server={proxy}")
navegador = webdriver.Chrome(service=servico, options=chrome_options)

navegador.get('https://tmail.link/inbox/[email protected]/')

CodePudding user response:

You should be able to get that href with the following locator:

link_url = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, '//a[text()="CLICK TO VERIFY EMAIL"]'))).get_attribute('href')

You will also need to import:

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

Selenium docs: https://www.selenium.dev/documentation/

CodePudding user response:

The element is a dynamic element so extract the value of the href to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using PARTIAL_LINK_TEXT:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, "CLICK TO VERIFY EMAIL"))).get_attribute("href"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., 'CLICK TO VERIFY EMAIL')]"))).get_attribute("value"))
    
  • Note : You have to add the following imports :

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

You can find a relevant discussion in Python Selenium - get href value

  • Related