【问题标题】:Python Selenium, find span in the buttonPython Selenium,在按钮中查找 span
【发布时间】:2021-01-09 02:20:27
【问题描述】:

我浪费了很多时间来弄清楚为什么司机不想点击这个按钮。我相信有人可以帮助我解决这个问题。 enter image description here

我需要点击“输入密码”。

我好累。我一直试图找出一个超过 10 小时的原因,但我仍然不能。我检查了很多关于 Selenium、xpath、span 等的线程。我尝试使用 execute_script() 启动此查找器,但它仍然无法也找不到此按钮并单击此按钮。

网站:https://e.mail.ru/login?email=

self.driver.find_element_by_id("//span[@class='inner-1-1-75 innerTextWrapper-1-1-76']")
"//span[contains(text(), 'Enter password')]//parent::button"
/span[contains(.,'Enter password')]

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:
    driver.get('https://e.mail.ru/login?email=')
    
    frame = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.TAG_NAME, "iframe"))
    )
    
    driver.switch_to_frame(frame)
    
    password = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, "//button[@data-test-id=\"next-button\"]"))
    )
    password.click()
    driver.switch_to_default_content()
    

    它在 iframe 内,你应该切换到它与它交互,一旦完成,你必须通过使用 switch 切换回外框来默认内容,以便能够再次与 iframe 之外的元素交互。

    也使用显式等待来避免加载问题

    【讨论】:

    • 感谢您的回答。 frame = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(By.TAG_NAME, "iframe")) TypeError: __init__() 接受 2 个位置参数,但给出了 3 个
    • 哦。 srry,我太笨了。我再次阅读了您的建议并像 driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) 一样进行测试,然后我尝试在 driver.page_source 中找到“输入密码”,因为当我拿了这个并试图找到时,我得到了没有。谢谢。
    • @rnate 如果有帮助,请点击答案中的勾号来接受答案
    【解决方案2】:

    我已经检查了网站。而且我认为这并不难。 你使用了错误的方法。您需要使用 find_element_by_xpath 方法。

    我是一名 ruby​​ 开发人员,所以想编写 ruby​​ 脚本。

    next_button = driver.find_element(:xpath, "//button[@data-test-id='next-button']")
    driver.execute_script("arguments[0].click()", next_button)
    

    在我看来,您的 xpath 不正确

    xpath = "//button[@data-test-id='next-button']"
    

    我觉得够了

    以下是用python脚本编写的完整答案

    next_button = self.driver.find_element_by_xpath("//button[@data-test-id='next-button']")
    self.driver.execute_script("arguments[0].click()", next_button)
    

    【讨论】:

    • 感谢您的回答。这个xpath我试过了,还是不行。
    猜你喜欢
    • 2019-11-18
    • 2020-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2021-08-12
    相关资源
    最近更新 更多