Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

Locating partial link text with python selenium in chrome headless mode

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...