2

i am using python+selenium simulate click option from drop menu

here is the drop menu picture enter image description here

and here is the pagesource of 3 different options

<a onclick="DigitSelected('TabMasterContent','N','sub_3','3587822','C')"><table class="chRT"><tbody><tr><td class="chC1p">C</td><td class="chC2p">Hastelloy C-22</td><td class="chC3p">526.00</td><td class="chC4p">73.64</td><td class="chC5p">0</td></tr></tbody></table></a>
<a onclick="DigitSelected('TabMasterContent','N','sub_3','3587823','D')"><table class="chRT"><tbody><tr><td class="chSC1p">D</td><td class="chSC2p">Hastelloy C-4 (2.4610)</td><td class="chSC3p">0.00</td><td class="chSC4p">0.00</td><td class="chSC5p">0</td></tr></tbody></table></a>
<a onclick="DigitSelected('TabMasterContent','N','sub_3','3587824','S')"><table class="chRT"><tbody><tr><td class="chC1p">S</td><td class="chC2p">Stainless steel 316</td><td class="chC3p">0.00</td><td class="chC4p">0.00</td><td class="chC5p">0</td></tr></tbody></table></a>

what I want to do is simply simulate click different options. i am using selenium IDE got the code should be like this

driver.find_element(By.CSS_SELECTOR, ".chRow:nth-child(2) .chC2p").click()

but i just simply want to use code select different options from the first letter of the options , "C", "D","S" , do you have any good ideas to do that? such as this one , I know it is not work

driver.find_element_by_partial_link_text("S").click
blackcat
  • 97
  • 1
  • 7

2 Answers2

1

Induce WebDriverWait and element_to_be_clickable() and following XPath.

WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[.//td[text()='C']]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[.//td[text()='D']]"))).click()
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[.//td[text()='S']]"))).click()

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
KunduK
  • 26,790
  • 2
  • 10
  • 32
1

As per the HTML you have shared all the child <td> tags seems to be with in individual <a> tags. Now as the elements are JavaScript enabled elements to click() on different options you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following xpath Locator Strategy:

  • Clicking on the row with Digit as C:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick,'DigitSelected')][./table/tbody/tr/td[text()='C']]")))
    
  • Clicking on the row with Digit as D:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick,'DigitSelected')][./table/tbody/tr/td[text()='D']]")))
    
  • Clicking on the row with Digit as S:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@onclick,'DigitSelected')][./table/tbody/tr/td[text()='S']]")))
    
  • 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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217