Locating partial link text with python selenium in chrome headless mode

726 Views Asked by At

I've finished my automated script with python selenium in Chrome normal mode and everything works great.

Until I decided to make it work in headless mode, so now I cannot locate a partial link text which already works in normal mode.

I'm using this code to open chrome in headless.

option = webdriver.ChromeOptions()
option.headless = True
option.AddArgument("window-size=1200,700");

This the code that I use to locate the element

tmp  = True
while tmp:
    try:
        Confirmm = driver.find_element_by_partial_link_text('Confirm')
        Confirmm.click()
        tmp = False
    except:
        continue

And this is the code of the link text I'm trying to click

<a href="https://temp-mail.org/en/view/346e2949a4a1f77ededd7542ba7947ed" title="" class="viewLink title-subject" data-mail-id="346e2949a4a1f77ededd7542ba7947ed">Confirm your profile</a>

NOTE: the data-mail-id= is not static and changes every time.

I tried to use Javascript but the word I'm willing to click doesn't have an ID, NAME, or TAG-NAME, and the ClassName doesn't work as well.

Any idea how to solve this?

2

There are 2 best solutions below

1
On BEST ANSWER

To click on the element with text as Confirm your profile you can use either of the following Locator Strategies:

  • Using link_text:

    driver.find_element_by_link_text("Confirm your profile").click()
    
  • Using partial_link_text:

    driver.find_element_by_partial_link_text("Confirm").click()
    
  • Using css_selector:

    driver.find_element_by_css_selector("a.viewLink.title-subject[href^='https://temp-mail.org/en/view']").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//a[@class='viewLink title-subject' and starts-with(@href, 'https://temp-mail.org/en/view')][contains(., 'Confirm your profile')]").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Confirm your profile"))).click()
    
  • Using PARTIAL_LINK_TEXT:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Confirm"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.viewLink.title-subject[href^='https://temp-mail.org/en/view']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='viewLink title-subject' and starts-with(@href, 'https://temp-mail.org/en/view')][contains(., 'Confirm your profile')]"))).click()
    
  • 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
    
0
On

Try to get screen shot of the page and verify whether link is available. Sometimes websites detects headless browsers and displays website differently.

  driver.get_screenshot_as_file(f"screenshot.png")

If the website displayed is different then set custom custom header (as website detects browser is headless using the useragent):

from selenium import webdriver


options = webdriver.ChromeOptions()

options.add_argument("--window-size=1920,1080")
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
browser = webdriver.Chrome(options=options)

Also use webdriver wait:

WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Confirm"))).click()