2

I am trying to understand Python in general as I just switched over from using VBA. I interested in the possible ways you could approach this single issue. I already went around it by just going to the link directly, but I need to understand and apply here.

from selenium import webdriver

chromedriver = r'C:\Users\dd\Desktop\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
url = 'https://www.fake.com/'

browser.get(url)

browser.find_element_by_id('txtLoginUserName').send_keys("Hello")
browser.find_element_by_id('txtLoginPassword').send_keys("There")
browser.find_element_by_id('btnLogin').click()

At this point, I am trying to navigate to a particular button/link. Here is the info from the page/element

 <a href="javascript:void(0)" style="text-decoration:none" onclick="InitiateCallBack('187', 'True', 'T-Mobile', 'https://www.fake.com/', 'TMobile')">T-Mobile</a>

Here are some of the things I tried:

for elem in browser.find_elements_by_xpath("//*[contains(text(), 'T-Mobile')]"):
    elem.click

browser.execute_script("InitiateCallBack(187, True, T-Mobile, https://www.fake.com/, TMobile)")

I also attempted to look for tags and use css selector all of which I deleted out of frustration!

Specific questions

  1. How do I utilize the innertext,"T-Mobile", to click the button?
  2. How would I execute the onclick event?

I've tried to read the following links, but still have not succeeded incoming up with a different way. Part of it is probably because I don't understand the specific syntax yet. This is just some of the things I looked at. I spent about 3 hours trying various things before I came here!

selenium python onclick() gives StaleElementReferenceException http://selenium-python.readthedocs.io/locating-elements.html Python: Selenium to simulate onclick https://stackoverflow.com/questions/43531654/simulate-a-onclick-with-selenium-https://stackoverflow.com/questions/45360707/python-selenium-using-onclick Running javascript in Selenium using Python

Noctsol
  • 478
  • 1
  • 8
  • 13

3 Answers3

3

How do I utilize the innertext,"T-Mobile", to click the button?

find_elements_by_link_text would be appropriate for this case.

elements = driver.find_elements_by_link_text('T-Mobile')
for elem in elements:
    elem.click()

There's also a by_partial_link_text locator as well if you don't have the full exact text.

How would I execute the onclick event?

The simplest way would be to simply call .click() on the element as shown above and the event should, naturally, execute at that time.

Alternatively, you can retrieve the onclick attribute and use driver.execute_script to run the js.

for elem in elements:
    script = elem.get_attribute('onlcick')
    driver.execute_script(script)

Edit:

note that in your code you did element.click -- this does nothing. element.click() (note the parens) calls the click method.

is there a way to utilize browser.execute_script() for the onclick event

execute_script can fire the equivalent event, but there may be more listeners that you miss by doing this. Using the element click method is the most sound. There may very well be many implementation details of the site that may hinder your automation efforts, but those possibilities are endless. Without seeing the actual context, it's hard to say.
You can use JS methods to click an element or otherwise interact with the page, but you may miss certain event listeners that occur when using the site 'normally'; you want to emulate, more or less, the normal use as closely as possible.

sytech
  • 29,298
  • 3
  • 45
  • 86
  • Thank you very much. I think it might be due to the way the website I'm testing is made, but It did not click on anything,but I got no errors back. It's a list of items/words that can be clicked like hyperlinks but can also be searched through. I'm certain that this works. – Noctsol Feb 27 '18 at 19:03
  • I forgot to ask but is there a way to utilize browser.execute_script() for the onclick event? – Noctsol Feb 27 '18 at 19:55
  • 1
    @Noctsol note that in your code you did `element.click` -- this does nothing. `element.click()` (note the parens) calls the click method. `execute_script` can fire the equivalent event, but there may be more listeners that you miss by doing this. Using the element click method is the most sound. There may very well be many implementation details of the site that may hinder your automation efforts, but those possibilities are endless. Without seeing the actual context for myself, it's hard to say. You can try using JS methods to click an element, but you may miss certain event listeners. – sytech Feb 27 '18 at 20:07
  • @sytech With all the words in your comment, I think you should update your Answer with all these ideas and turn your Answer into a Great Answer. Upvoting !!! – undetected Selenium Mar 16 '18 at 05:23
  • 1
    @DebanjanB thanks for the suggestion. I added that to the answer. – sytech Mar 16 '18 at 05:29
1

As per the HTML you have shared it's pretty clear the website uses JavaScript. So to click() on the link with text as T-Mobile you have to induce WebDriverWait with expected_conditions clause as element_to_be_clickable and your can use the following code block :

WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.XPATH, "//a[contains(.,'T-Mobile')]"))).click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

you can use it

 <div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
    <input class="v_small" type="button"></input>
    <span>
          Reset
    </span>

Firat C
  • 11
  • 2