Home > database >  What is the best way to click on element with selenium where id is changing dynamically?
What is the best way to click on element with selenium where id is changing dynamically?

Time:11-05

I have web-page where I am trying to automate publishing of products with Selenium.

You can check situation here: https://player.vimeo.com/video/767368522?h=a8feaf8791

I am able to click onto everything else but this button (because the id the number part is automatically generated).

I've thought of selecting it by class_name, by XPATH but it seems like it's just not ok...

This is the HTML structure:

<div id="mceu_104"  hidefocus="1" role="dialog" aria-describedby="mceu_104-none" aria-label="Source code" style="border-width: 1px; z-index: 65536; left: 447px; top: 34px; width: 640px; height: 361px;">
   <div  role="application">
      <div id="mceu_104-head" >
         <div id="mceu_104-title" >Izvorni kod</div>
         <button type="button"  aria-hidden="true">×</button>
         <div id="mceu_104-dragh" ></div>
      </div>
      <div id="mceu_104-body"  style="width: 640px; height: 271px;">
         <div id="mceu_104-absend" ></div>
         <div id="mceu_105"  style="left: 0px; top: 0px; width: 640px; height: 271px;">
            <div id="mceu_105-body"  style="width: 640px; height: 271px;">
               <div id="mceu_105-absend" ></div>
               <textarea id="mceu_106"  hidefocus="1" spellcheck="false" style="direction: ltr; text-align: left; left: 20px; top: 20px; width: 590px; height: 221px;"></textarea>
            </div>
         </div>
      </div>
      <div id="mceu_107"  hidefocus="1" tabindex="-1" role="group" style="border-width: 1px 0px 0px; left: 0px; top: 0px; width: 640px; height: 50px;">
         <div id="mceu_107-body"  style="width: 640px; height: 50px;">
            <div id="mceu_107-absend" ></div>
            <div id="mceu_108"  tabindex="-1" aria-labelledby="mceu_108" role="button" style="left: 508.975px; top: 10px; width: 58.025px; height: 28px;"><button role="presentation" type="button" tabindex="-1" style="height: 100%; width: 100%;"><span >U redu</span></button></div>
            <div id="mceu_109"  tabindex="-1" aria-labelledby="mceu_109" role="button" style="left: 572px; top: 10px; width: 56px; height: 28px;"><button role="presentation" type="button" tabindex="-1" style="height: 100%; width: 100%;"><span >Otkaži</span></button></div>
         </div>
      </div>
   </div>
</div>

So I would need to click onto this element:

<button role="presentation" type="button" tabindex="-1" style="height: 100%; width: 100%;"><span >U redu</span></button>

This is my code:

 final_description = html_and_css   csvproductDescription   html_and_css2
    WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.CLASS_NAME, 'mce-textbox'))).send_keys(final_description)

    WebDriverWait(driver, 2).until(EC.visibility_of_element_located((By.ID, 'mceu_108'))).click()

Can anyone help me, as this seems to be using tinymce and these ID's are generating automatically...

Thanks!

CodePudding user response:

To click on the button U redu you can use any of the xpath option

WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, '//button[.//span[text()="U redu"]]'))).click()

OR

WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, '//button[contains(.,"U redu")]'))).click()

CodePudding user response:

Maybe you will need get this element by XPATH:

Try this: from selenium import webdriver >>> Just for Webdriver from selenium.webdriver.common.by import By >>> This will help u to get the XPATH

Exeple: webdrider.find_element(By.XPATH, 'Put the XPATH here').click()

Observation: Always put '' in XPATH because in some occasions the Id of XPATH will have "".

Hope this helps.

  • Related