0

I'm trying to automate adding requirements to a TestLink database. I'm running into an issue trying to click on this anchor/span.

<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span>
</a>

Here is the section of Python code with things I've tried:

anchor = browser.find_element_by_xpath('//a[contains(@href, "REQ_SPEC_MGMT")]')
span = anchor.find_element_by_xpath('.//span')

anchor.click()  # Doesn't work
span.click()    # Doesn't work
browser.execute_script("arguments[0].click();", anchor)  # Doesn't work
browser.execute_script("arguments[0].click();", span)    # Doesn't work

I don't get any errors, but I still don't get the page that appears when I manually click on the link. I verified that I'm finding the correct anchor/span by dumping out the properties so I know I have the correct elements. I've also tried long pauses just to make sure that the element is clickable before I try it. Any ideas on what I am doing wrong? Thanks!

Update - Here's a larger section of the HTML:

<div id="tree_div" style="overflow:auto; height:100%;border:1px solid #c3daf9;" class=" x-panel x-tree">
<div class="x-panel-bwrap" id="ext-gen12"><div class="x-panel-body x-panel-body-noheader" id="ext-gen13" style="overflow: auto;">
<ul class="x-tree-root-ct x-tree-arrows" id="ext-gen14">
<li class="x-tree-node"><div ext:tree-node-id="17472" class="x-tree-node-el x-unselectable x-tree-node-expanded" unselectable="on" id="extdd-1">
<span class="x-tree-node-indent"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-end-minus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-2">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:TPROJECT_REQ_SPEC_MGMT(17472)" tabindex="1">
<span unselectable="on" id="extdd-3">ProjTasks (0)</span>
</a>
</div>
<ul class="x-tree-node-ct" style="">
<li class="x-tree-node">
<div ext:tree-node-id="17473" class="x-tree-node-el x-unselectable folder x-tree-node-collapsed" unselectable="on" id="extdd-4">
<span class="x-tree-node-indent"><img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-icon"></span>
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-ec-icon x-tree-elbow-plus">
<img alt="" src="http://192.168.11.111/third_party/ext-js/images/default/s.gif" class="x-tree-node-icon" unselectable="on" id="extdd-5">
<a hidefocus="on" class="x-tree-node-anchor" href="javascript:REQ_SPEC_MGMT(17473)" tabindex="1">
<span unselectable="on" id="extdd-6">0:Project-0 (0)</span></a></div><ul class="x-tree-node-ct" style="display:none;">
</ul>
</li>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
jmq
  • 1,559
  • 9
  • 21
  • can you share the page url? or the little bit more html source around that element? – Sureshmani Kalirajan Sep 04 '19 at 15:22
  • The TestLink application is running on an internal network not visible to the public. However, TestLink is open source if anyone is eager to download the code :-). I'll add more source to the question. – jmq Sep 04 '19 at 15:25
  • 1
    Wierdly, there is an attribute called unselectable which is set to 'on' – work_ishaan Sep 04 '19 at 15:35
  • 1
    @work_ishaan Good observation. I found https://stackoverflow.com/a/38753449/7218062 which may be relevant. I'm going to try it. – jmq Sep 04 '19 at 15:45

3 Answers3

0

At first, try to use implicit selenium wait, to make sure that all your elements is download.

anchor = WebDriverWait(browser,30).until(EC.element_to_be_clickable((By.XPATH, '//a[contains(@href, "REQ_SPEC_MGMT")]')))
span = WebDriverWait(anchor,30).until(EC.element_to_be_clickable((By.XPATH, './/span')))

If it does'n work fine, try to execut js script in href:

browser.execute_script("javascript:REQ_SPEC_MGMT(17473)") 

or

browser.execute_script(anchor.get_attribute("href")) 
brfh
  • 334
  • 1
  • 12
0

I figured out the issue. Some of the characters I was passing to execute_script were being dropped by my vnc software (Python Selenium script drops keys but only when used on a VNC).

jmq
  • 1,559
  • 9
  • 21
0

To click() on the element with text as 0:Project-0 (0) element is an JavaScript enabled element so to click() on the element 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, "a.x-tree-node-anchor[href*='REQ_SPEC_MGMT']>span[id^='extdd-']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='x-tree-node-anchor' and contains(@href, 'REQ_SPEC_MGMT')]/span[starts-with(@id, 'extdd-') and text()='0:Project-0 (0)']"))).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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • `I've also tried long pauses just to make sure that the element is clickable before I try it.` right from the question text... – JeffC Sep 06 '19 at 14:37