【发布时间】:2018-08-07 12:20:06
【问题描述】:
我刚从使用 VBA 切换过来时,正在尝试总体上理解 Python。我对您可以解决这个单一问题的可能方式感兴趣。我已经通过直接访问链接绕过它,但我需要在这里理解和应用。
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()
此时,我正在尝试导航到特定按钮/链接。 这是来自页面/元素的信息
<a href="javascript:void(0)" style="text-decoration:none" onclick="InitiateCallBack('187', 'True', 'T-Mobile', 'https://www.fake.com/', 'TMobile')">T-Mobile</a>
以下是我尝试过的一些事情:
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)")
我还尝试查找标签并使用 css 选择器,我因沮丧而删除了所有这些标签!
具体问题
- 如何利用内文“T-Mobile”点击按钮?
- 如何执行 onclick 事件?
我已经尝试阅读以下链接,但仍然没有成功以不同的方式进入。部分原因可能是因为我还不了解具体的语法。这只是我看过的一些东西。来这里之前,我花了大约 3 个小时尝试各种事情!
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
【问题讨论】: