1

I am trying to automate a company website where I have to make specific trades. If I want to make one trade at a time rather than in bulk I need to submit a trade and then reload the webpage to submit another trade. Unfortunately the button I need to click to pick my trade is extremely generic and only has an index to reference the object I wish to trade (I do not know the index prior and they change over time). As a result I have to find another specific element nearby and do an offset click with ActionChains. Unfortunately I am unable to use find_element_by _link_text because there could be several of the same name occupying different dates. As a result I needed to use an specific XPATH that include the date and name.

This is the XPATH I needed to use and then offset as it is all I could find with my specific XName and XDate.

//*[@onclick='javascript:pair("XName","XDate",0,0,"",0,"BC")']

If I run the below code twice it will work the first try. The second time it will give the evil StaleElementReferenceException.

AC = ActionChains

def two4one(XName, XDate):
    browser.execute_script('''window.open("link.com", "_blank");''')
    browser.switch_to.window(browser.window_handles[-1])

    AC.move_to_element_with_offset(browser.find_element(By.XPATH, f'''//*[@onclick='javascript:pair("{XName}","{XDate}",0,0,"",0,"BC")']'''), -75,0).click()
    AC.perform()

If I run the above code once and then the code below right after it can find the element with no issues and even click the link. Sadly the link this clicks on is what I am offsetting from and not my end goal which is 75 pixels to the left.

def test241(XName, XDate):
    browser.execute_script('''window.open("link.com", "_blank");''')
    browser.switch_to.window(browser.window_handles[-1])

    bob = browser.find_element_by_xpath(f'''//*[@onclick='javascript:pair("{XName}","{XDate}",0,0,"",0,"BC")']''')
    bob.click()

I was using a the new tabs for troubleshooting purposes. I haven't changed the code back yet.

I expect to be able to run my two4one() function multiple times with different variables but unfortunately it seems that when I use ActionChains it causes a StaleElementReferenceException. I can still find the element with browser.find_element() yet Action Chains can not. I have even made a variable to plug into AC that prints prior to my AC code and it will print just fine then throw the error once it hits the AC code.

I have honestly tried tons of different things such as forcing a loop. WebDriverWait throws the exception immediately rather than actually waiting for the element to be present. I honestly have no idea how to make this work.

EDIT:

The fixes listed as duplicates do not work. PageFactory from what I can tell is not available in Python. The PageFactory link was for Java. The other "fix" does not work because WebDriverWait does not actually wait. I have yet to get a timeout error. As soon as my code gets to this line

WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, f'''//*[@onclick='javascript:pair("{item1}","{item2}",0,0,"",0,"CA")']''')))

it throws the StaleElementReferenceException and does not wait 10 seconds prior to giving me the exception. This is what is confusing me. I can find the element no problem with

browser.find_element(By.XPATH, f'''//*[@onclick='javascript:pair("{item1}","{item2}",0,0,"",0,"CA")']''')

but once I try another way to find the element or even use the element from the browser.find_element() code it goes stale immediately.

EDIT: I found a work around. I just iterate through the list of trades still it matches what I want. I created a counter that adds up each iteration which then corresponds with the index of the button I need to click. I then used the counter value in the XPATH that determines the button I need to click on.

Hopefully that makes sense. Just kind of annoying that ActionChains didn't want to work. For some reason I thought the iterations would take longer but they seem to be quicker. I tried my program live yesterday but messed up my inputs. Woops.

Link200
  • 11
  • 3
  • The index you mentioned it changes by, is there any other elements which could be mistaken for this button (so if you got it wrong for example), if you did in-fact manage to find the right index? – David Silveiro Apr 06 '19 at 00:43
  • If I could get the correct index it should work. If I get the wrong index I can cancel prior to it processing but I will miss my time sensitive window. Basically what I have is a table of events. The first column has the "select" button I need to hit (the only difference between each button is the index). The second column has the Event Name (this is also a link for more information about the event. This is the element I use because the XPATH includes the date ). The third column is the date of the event. – Link200 Apr 06 '19 at 07:13

0 Answers0