Joe Rivera
Joe Rivera

Reputation: 363

Python Selenium issue while clicking an element

I have the below code I've inspected from Chrome. and I am trying to click this object. Ideally leveraging xpath or css selector.

I copied Xpath as below mentioned

/html/body/form/table[4]/tbody/tr/td[1]/table/tbody/tr[3]/td

This is the full path

<td style="text-decoration: none; color: rgb(0, 0, 0); font-family: Arial; font-size: 11px; padding-bottom: 2px; padding-top: 1px; border-bottom: 1px solid rgb(194, 194, 194); background: rgb(255, 255, 255); cursor: auto;" onmouseover="this.style.background='#BED9F5'; this.style.cursor='hand'; showTitle(this,'Review Quotes / Quote To Order',150,true); window.status='Review Quotes / Quote To Order'; return true;" onmouseout="this.style.background='#FFFFFF'; this.style.cursor='auto'; hideTitle(); window.status=' '; return true;" onclick="if (warnUnfinishedPage()) return false; changePage('pc.quote.html.QuoteSummarySection','refresh');  return false;" align="left" width="110" valign="middle" title="Review Quotes / Quote To Order">

&nbsp;&nbsp;Quotes

</td>

Katalon Recorder exported Python as the below few options, but none seem to be working for me. They all come back as no such element. I think it may have something to do with the parentheses?

driver.find_element_by_xpath("//td[@onclick=\"if (warnUnfinishedPage()) return false; changePage('pc.quote.html.QuoteSummarySection','refresh'); return false;\"]").click()

driver.find_element_by_xpath("//tr[3]/td").click()

driver.find_element_by_css_selector("td[title=\"Review Quotes / Quote To Order\"]").click()

Any advice on something else I can try would be much appreciated!

Upvotes: 1

Views: 129

Answers (2)

Joe Rivera
Joe Rivera

Reputation: 363

The issue was due to a multitude of things. One I needed to implement a command to change active tabs.

The automation I was performing opened up a new tab so I had to leverage the below code to activate that tab.

driver.switch_to.window(driver.window_handles[1])

I also needed to implement a little wait for things to load

driver.implicitly_wait(3)

I also needed to account for iframes. I had multiple iframes so I used the below to print out how many I had

seq = driver.find_elements_by_tag_name('iframe')
    print("Number of frames present in the web page are: ", len(seq))

Then I was able to figure out I had 2 frames and the below code looped through each until the xpath was discovered.

    for x in range(2):
        try:
            driver.switch_to.frame(x)
            driver.find_element_by_xpath("//tr[3]/td").click()
        except:
            print("It's not: ", x)
            continue

Upvotes: 1

undetected Selenium
undetected Selenium

Reputation: 193348

The desired element is a JavaScript enabled element so you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "td[title='Review Quotes / Quote To Order']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@title='Review Quotes / Quote To Order' and contains(.,'Quotes')]"))).click()
    
  • Note : You have to add the following imports :

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

Upvotes: 1

Related Questions