I am using given below xpath but my script is not clicking on these element (python, scrapy, selenium)

driver.find_element(By.XPATH, '//*[@id="MiniReport2"]/thead/tr/td[@class="Over" and contains(@onclick,"MenuClick")]')

HTML Sample:

<table id="MiniReport2" border="0" cellspacing="0" cellpadding="0" class="Menu Unit" style="">            
            <thead>                
                <tr>                    
                    <td onmouseover="className='Over'" onmouseout="className=''" onclick="javascript:MenuClick('MiniReport2');" class="">
                         <div class="Plus">4. Report</div>                    
                    </td>                
                </tr>            
            </thead>            
</table>

2 Answers

0
supputuri On

Welcome to SO.

Here is the xpath that you can use.

//table[@id='MiniReport2']//td[contains(@onclick,'MenuClick')]

Here is the script (I guess you are using python)

driver.find_element_by_xpath("//table[@id='MiniReport2']//td[contains(@onclick,'MenuClick')]").click()

If you want to click on 4. Report then use the below

driver.find_element_by_xpath("//table[@id='MiniReport2']//td[contains(@onclick,'MenuClick')]/div[normalize-space(.)='4. Report']").click()
0
Kajal Kundu On

To handle dynamic element use WebdriverWait and following locator strategy.

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

element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,'//table[@class="Menu Unit"][starts-with(@id,"MiniReport")]//tr//td[contains(@onclick,"MenuClick")]/div[@class="Plus"][contains(.,"4. Report")]')))
element.click()