【问题标题】:How to click the specific button with Selenium and Python?如何使用 Selenium 和 Python 单击特定按钮?
【发布时间】:2021-04-07 16:55:26
【问题描述】:

我正在尝试查找并单击此按钮。但我总是面对“消息:无法找到元素:”

我尝试了这个选项,但它对我没有帮助:

第一:driver.find_element_by_xpath('//button[contains(text(), "CLAIM NOW!")]').click()

二:driver.find_element_by_xpath('//button[@onclick="getClaim()"]').click()

第三:driver.find_element_by_css_selector('a.btn').click()

第四:driver.find_element_by_class_name('btn btn-lg btn-primary').click()

第五:driver.find_element_by_id('roll_button_container').click()

我也使用time.sleep(),所以它不是时间问题

操作流程:点击“立即申请!”后一个新标签将 开放,但在前一个(我现在在) - 我会得到积分。 我需要在项目中自动执行此操作

我几乎完整的代码(不能向您展示公司政策的所有内容):

from selenium  import webdriver
import time

driver = webdriver.Firefox (executable_path = "/Users/USER1/Downloads/AUTOMATION/geckodriver")

driver.get("https://LINK.net/")
time.sleep(3)
driver.find_element_by_xpath('/html/body/header/nav/div/div/ul/li[5]/a').click()
time.sleep(3)
element = driver.find_element_by_xpath('/html/body/div[3]/div/div/form/div[1]/input').send_keys("login")
time.sleep(3)
el2 = driver.find_element_by_xpath('/html/body/div[3]/div/div/form/div[2]/input').send_keys("password")
time.sleep(3)
driver.find_element_by_css_selector('#loginForm > div:nth-child(3) > button:nth-child(1)').click()
time.sleep(3)
driver.find_element_by_xpath('//*[@id="test_button"]').click()
time.sleep(3)
#driver.find_element_by_xpath('//button[contains(text(), "CLAIM NOW!")]').click()
#driver.find_element_by_xpath('//button[@onclick="getClaim()"]').click()
#driver.find_element_by_class_name('btn btn-lg btn-primary').click()
#driver.find_element_by_id('roll_button_container').click()
driver.find_element_by_css_selector('a.btn').click()
time.sleep(3)
driver.quit()

while True:
  time.sleep(60 * 5)

有人可以帮我正确点击按钮吗?非常感谢。

【问题讨论】:

  • getClaim() 里面有什么?
  • 我加了截图,大家可以看看。如您所见 - 它是空的,但我不知道您的意思,抱歉
  • 一个 URL 会更有帮助,该屏幕截图不包含有用的数据。您想要点击的js函数getClaim可能包含一个页面,您可以在不模拟按钮点击的情况下加载该页面。
  • 我需要点击按钮,而不是去其他页面
  • 好的,给我们看看你的代码,不要把 URL 弄糊涂了!我们无法以这种方式帮助您。

标签: python selenium selenium-webdriver geckodriver findelement


【解决方案1】:

好的,看起来该页面会将您重定向到一个充满虚假广告的页面。通过点击链接,每次都会弹出一个随机广告。或者,您可以使用 selenium 运行 getClaim js

可以通过模拟按钮按下来领取奖励 - 运行按钮将运行的功能 - only running javascript with selenium

cmd = "getClaim()"
driver.execute_script(cmd)

我希望,正如你所说,一个“可怜的印度人”可以帮助你。

【讨论】:

    猜你喜欢
    • 2021-03-31
    • 2013-11-12
    • 1970-01-01
    • 2018-08-22
    • 2019-09-01
    • 2017-06-03
    • 2016-12-31
    • 2019-09-16
    • 2018-04-30
    相关资源
    最近更新 更多