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

佐手、 提交于 2019-12-09 03:23:32

问题


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.


回答1:


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
    


来源:https://stackoverflow.com/questions/55950278/find-and-click-element-which-has-a-changing-css-selector-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!