简体   繁体   中英

Dynamic website find element by class name and execute onclick method associated with the class in Python with Selenium and Chrome

I'm working on Python, Selenium and Chrome driver. I have a dynamic website and every time site loads the IDs get changed, Hence I can't use Xpath. But I can use the class name. I can get to the class as below; Following is the code of the site.

<a class="appButton registerItemSearch-tabs-criteriaAndButtons-buttonPad-search appSearchButton appPrimaryButton appButtonPrimary appSubmitButton appNotReadOnly appIndex2" id="nodeW830" href="#" onclick="return function(me){var retVal = false; if (catHtmlFragmentCallback('W830','buttonPush',null,{asyncUpdate:true,containerSelector:'#AsyncWrapperW814',containerNodeId:'W814',success:function(html){jQuery('#AsyncWrapperW814').empty().append(html);webuiAsyncOk('#AsyncWrapperW814');}}, me) == 'skip') retVal = true;return retVal;}(this)" tabindex="118"><span class="left"></span><span class="appReceiveFocus" tabindex="-1">Search</span><span class="right"></span></a>

However I can do the following; to get to the element.

xxx = driver.find_elements_by_class_name("appButton.registerItemSearch-tabs-criteriaAndButtons-buttonPad-search.appSearchButton.appButtonPrimary.appPrimaryButton.appSubmitButton.appNotReadOnly.appIndex2")

Now I want to execute onclick method associated with the class. I can do that as well like below;

driver.execute_script("return function(me){var retVal = false; if (catHtmlFragmentCallback('W830','buttonPush',null,{asyncUpdate:true,containerSelector:'#AsyncWrapperW814',containerNodeId:'W814',success:function(html){jQuery('#AsyncWrapperW814').empty().append(html);webuiAsyncOk('#AsyncWrapperW814');}}, me) == 'skip') retVal = true;return retVal;}(this)")

But as I mentioned, I can't hardcode the driver.execute_script values. I should get the values dynamically. Could you please help me? If you can suggest a way, that I can find the element by class name and execute onclick method. or as I mentioned; I can find the element by class name, if I can get the code (HTML)associated with the element then I can do driver.execute_script. Can you please suggest a way forward. Thank you

Induce WebDriverWait and element_to_be_clickable () and following locator.

Xpath :

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//a[.//span[text()='Search']]"))).click()

CSS selector :

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.appButton.registerItemSearch-tabs-criteriaAndButtons-buttonPad-search.appSearchButton.appPrimaryButton.appButtonPrimary.appSubmitButton.appNotReadOnly.appIndex2"))).click()

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM