0
size = '19'

driver.find_element_by_xpath('//a[@title="Sélectionner Taille:" + size]').click()

this is my code, but it doesn't work as the ' stuck the variable into a simple quotation so I don't know how to do, please help me I use selenium with python into chrome driver

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Tom large
  • 11
  • 1

2 Answers2

0

try this it will help you.

driver.find_element_by_xpath("//a[@title="Sélectionner Taille:"'" + size + "']").click()

You can also refer to this answer as well: link

0

Without the HTML it is not clear about the number of white spaces between Sélectionner Taille: and 19. So discounting all the white spaces to invoke click() on the element with respect to the variable using Selenium and you need to to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using variable in XPATH:

    size = '19'
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@title, 'Sélectionner Taille') and contains(@title, '" +size+ "')]"))).click()
    
  • Using %s in XPATH:

    size = '19'
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@title, 'Sélectionner Taille') and contains(@title, '%s')]"% str(size)))).click()
    
  • Using format() in XPATH:

    size = '19'
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@title, 'Sélectionner Taille') and contains(@title, '{}')]".format(str(size))))).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
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352