Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium webdriver 如何单击带有文本的按钮';当然';使用Selenium和Python_Selenium Webdriver_Xpath - Fatal编程技术网

Selenium webdriver 如何单击带有文本的按钮';当然';使用Selenium和Python

Selenium webdriver 如何单击带有文本的按钮';当然';使用Selenium和Python,selenium-webdriver,xpath,Selenium Webdriver,Xpath,我在开门时有问题 在firefox上使用selenium。我正在使用python 我想运行此代码以单击“确定!”按钮,但它确实有效,只是没有错误 救命啊! 我的代码是 driver.find_element_by_xpath("//button[@class='btn theme--dark primary'][@type='button']").click() 请注意,当我执行此代码时,包含“SURE”按钮的弹出层消失,但SURE按钮操作未应用。好像我正在单击ESC import tim

我在开门时有问题

在firefox上使用selenium。我正在使用python

我想运行此代码以单击“确定!”按钮,但它确实有效,只是没有错误

救命啊!
我的代码是

driver.find_element_by_xpath("//button[@class='btn theme--dark primary'][@type='button']").click()
请注意,当我执行此代码时,包含“SURE”按钮的弹出层消失,但SURE按钮操作未应用。好像我正在单击ESC

import time
from selenium import webdriver

driver = webdriver.Chrome(executable_path="C:/Users/myname/Desktop/Projects/chromedriver.exe")
driver.get("https://pribot.org/polisis/?company_url=http%3A%2F%2Fhotmail.com&status=policyAbsentRedirect")
time.sleep(5)
driver.find_element_by_xpath('//*[@id="app"]/div[2]/div/div/div[2]/div/div/button[2]').click() 
使用上面的代码片段,对我来说效果很好。 您需要明确地等待3-5秒。 希望能有帮助!! 如果没有,请尽快通知我们。

根据您问题中的with,您需要引导WebDriverWait使所需元素可单击,并且您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("https://pribot.org/polisis/?company_url=http%3A%2F%2Fhotmail.com&status=policyAbsentRedirect")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dialog dialog--active dialog--persistent']//button[@class='btn theme--dark primary']/div[@class='btn__content']"))).click()
    
  • 浏览器快照:


您询问如何单击包含文本“确定!”的元素。唯一的方法是使用指定所需文本的XPath。您可以使用下面的XPath

//div[.='Sure!']
由于该弹出窗口最初不会出现在页面加载上,因此您需要添加等待,例如

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[.='Sure!']"))).click()