Vanassins
Vanassins

Reputation: 73

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

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 called:

PPTAmFCTable > tbody:nth-child(1) > tr:nth-child(11) > td:nth-child(3) > a:nth-child(1)

Yesterday it was:

PPTAmFCTable > tbody:nth-child(1) > tr:nth-child(10) > td:nth-child(3) > a:nth-child(1)

And tomorrow it might be tr:nth-child(13) or so.

I use the following code:

def click_element_bycss(browser,css,timeout):
    element_present = EC.presence_of_element_located((By.CSS_SELECTOR,css)) 
    WebDriverWait(browser, timeout).until(element_present)
    browser.find_element_by_css_selector(css).click()

and then:

click_element_bycss(browser,"#PPTAmFCTable > tbody:nth-child(1) > tr:nth-child(11) > td:nth-child(3) > a:nth-child(1)",4)

How can I write the code in a way that I click on the right element, w/o knowing the number in the second tr:nth-child() ?

Element HTML:

<a href="/FC1/ItemList;jsessionid=E6B3D538CD809FDDC3DE69EA160C956D?WorkPool=PickingNotYetPicked&amp;ExSDRange.RangeEndMillis=1556850660000&amp;ProcessPath=PPTAmFC&amp;ExSDRange.RangeStartMillis=1556850599999&amp;shipmentType=TRANSSHIPMENTS">261</a>

Thought of a loop which just runs through for i in range(1,20): tr:nth-child(i) but would expect there's something smarter.

Upvotes: 2

Views: 776

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193098

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
    

Upvotes: 3

Related Questions