简体   繁体   中英

Not able to click on drop down option in a ajax webpage using selenium python

I am able to click on the drop down heading but not able to click on the options. Also I am not able to identify the 'id' or xpath for options which is visible after clicking the dropdown option.

Find the HTML below:

<div id="object260310" style="position: absolute; overflow: hidden; background: transparent; z-index: 50; left: 154px; top: 5px; width: 74px; height: 20px;">
  <div id="object351" style="position: absolute; z-index: 11; left: 0px; top: 0px; width: 74px; height: 20px; background-color: rgb(255, 255, 255);">
    <div style="position: absolute; width: 42px;">
      <div role="menu" aria-label="1" class="font89" style="padding-left: 0px; cursor: pointer; position: absolute; left: 0px; color: rgb(126, 126, 126); width: 43px; height: 20px; line-height: 20px; background: transparent;" onclick="plw.menu.click(this,351,0,&quot;1&quot;,true);" onmouseenter="plw.menu.over(event,this,351,0,&quot;1&quot;);this.style.color=&quot;rgb(174,174,174)&quot;;this.style.backgroundColor=&quot;rgb(255,255,255)&quot;" onmouseleave="plw.menu.out(351);this.style.background=&quot;transparent&quot;;this.style.color=&quot;rgb(126,126,126)&quot;;">
        <div style="position:absolute;left:0px;top:2px" class="image347 "></div>
        <span style="position:relative;left:21px;top:0px">New</span>
      </div>
    </div>
  </div>
</div>

Below is my selenium code:

new_create = WebDriverWait(driver, 40).until( EC.presence_of_all_elements_located((By.XPATH, '/html/body/div[2]/div[2]/div[5]/div/div/div/div')))
driver.find_element_by_id("object260310").click()
#its working fine till here

driver.find_element_by_xpath(".//*[contains(@onclick, '231')]").click() 
#This line doesnt seems to work.

And here is a screenshot of the site .

Clicking drop down options have been iffy for me before. This is a thing I do when there aren't any other solutions:

Click the drop down then you can send keys depending on the letter of the option you need. so if an option is "action" then you press "a".

This highly depends on what you have in the drop down though.

EDIT: I would highly look at all the related questions there... They have some stuff you might be able to use. As I said this is only if those don't work!

  1. You can try to click on the drop-down first(New in your case)
  2. If the dropdown is not select , then use the below code select the value.

     driver.find_element_by_xpath("//div[text(),'Product Variation']").click(); 
driver.find_element_by_xpath("//div[contains(text(), 'Product Variation']").click(); 

Think the xpath should be written that way. Will you send me the URL and I'll make sure I can click before I make another edit on this post?

Thank you all for your help. I was able to find the xpath using chrome XPATH Helper extension.Extension gave below xpath :

/html/body/div[@id='m235e0-SUB-1']/table[@class='oldmenu']/tbody/tr[@id='235-0-SUB-1-1']/td[@class='oldmenu']

Using this I was able to identify the id for sub menu ie '235-0-SUB-1-1'

So I modified the code accordingly as below :

driver.find_element_by_xpath('//*[@id="235-0-SUB-1-1"]').click()

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