find and click element which has a changing CSS selector (python)

后端 未结 1 1904
不思量自难忘°
不思量自难忘° 2021-01-15 19:21

I am writing a script which needs to click on an element of a page, however, the CSS selector changes everyday as the element changes it\'s location.

Today it\'s cal

相关标签:
1条回答
  • 2021-01-15 20:04

    To find and click on the element as the element is a dynamic element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#PPTAmFCTable>tbody a[href^='/FC1/ItemList'][href$='TRANSSHIPMENTS']"))).click()
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='PPTAmFCTable' and starts-with(@href,'/FC1/ItemList')][contains(@href, 'TRANSSHIPMENTS')]")))
      
    • 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 讨论(0)
提交回复
热议问题