Python selenium identify web element using custom attributes

Viewed 36

I have the custom attribute called upgrade-test="secondary-pull" in the following script:

<section class="dvd-pull tech-pull-- secondary-pull--anonymous tech-pull--digital secondary-pull--dvd-ping tech-pull--minimise" upgrade-test="secondary-pull" data-js="primary-pull" style="--primary-direct-d_user-bottom-pos:-290px;">

I wish to use this attribute to identify the element in question. I have tried the following, but unfortunately the script did not work:

element = driver.find_element(By.XPATH, "//class[contains(@vlaue, 'secondary-pull')]") 

Any help would really be appreciated. Thanks

1 Answers

I think you need to know about how to identify and use xpath

Given the html <section class="dvd-pull tech-pull-- secondary-pull--anonymous tech-pull--digital secondary-pull--dvd-ping tech-pull--minimise" upgrade-test="secondary-pull" data-js="primary-pull" style="--primary-direct-d_user-bottom-pos:-290px;">

xpath should be like

//node[@attribute_name='attribute_value']

in your case xpath should have been

//section[@upgrade-test='secondary-pull']

Ideally your python code should have been

element = driver.find_element(By.XPATH, "//section[@upgrade-test='secondary-pull']")

There can be many ways to do that. Take one more xpath

element = driver.find_element(By.XPATH, "//section[contains(@class,'secondary-pull')]")
Related