New version

我想要浏览一下显示在这个link上的条款和条件弹出消息,无论是单击"同意"按钮还是"X"按钮关闭窗口,usign python-Selify.

经过一些反馈,我知道现在我没有处理iframe,但问题是我无法获得任何这些按钮,似乎我必须改变焦点或类似的东西.

#======================
#Sets libraries
#======================
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common import action_chains
import time
from datetime import datetime

#======================
#Gets into the webpage and breaks the captcha
#======================
#Sets chr_options to pass the cloudflare captcha
chr_options = webdriver.ChromeOptions()
chr_options.add_experimental_option("detach", True)
chr_options.add_experimental_option("excludeSwitches", ["enable-automation"]) #Hides selenium browser features
chr_options.add_experimental_option('useAutomationExtension', False) #Hides selenium browser features
chr_options.add_argument("--disable-blink-features=AutomationControlled") #Hides testing browser features
chr_options.add_argument('--disable-extensions') #Disable extensions which may carry scripts
chr_options.add_argument('--no-sandbox') #Disable developer/sandbox mode
chr_options.add_argument('--disable-infobars') #Disable information bar
chr_options.add_argument('--disable-dev-shm-usage') #Disable memory optimization features
chr_options.add_argument('--disable-browser-side-navigation') #Disable memory optimization features
chr_options.add_argument('--disable-gpu') #Disable graphic features

#Sets the webdriver
chrdr_path="C:\Program Files (x86)\chromedriver.exe"
chrome_service=Service(executable_path=chrdr_path)
driver=webdriver.Chrome(service=chrome_service,options=chr_options)

#Opens 2 pages in the browser to simulate human behaviour
driver.execute_script("window.open('https://beacon.schneidercorp.com/Application.aspx?AppID=1015&LayerID=21105&PageTypeID=2&PageID=9036', '_blank')")
#Waits 15 seconds
time.sleep(15)
#Moves to beacon schneider window
driver.switch_to.window(driver.window_handles[1])
#Goes to the captcha frame
driver.switch_to.frame(0)
#Clicks on the chaptcha checkbox
driver.find_element(By.XPATH, '//*[@id="challenge-stage"]/div/label/input').click()

#Here I found the popup message which doens't let me pass
time.sleep(20)
# WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Agree'")))
popup_button=driver.find_element(By.XPATH,"//a[text()='Agree']")
popup_button.click()

在最后几行中,我一直在增加时间,设置显式等待,设置不同的等待方法(用于在线状态和可点击性),但它总是在搜索By.XPATH,"//a[text()='Agree']"的那一行崩溃

看起来很直截了当,但我似乎不知道如何解决这个问题

Original Version

我想在这条消息上点击"同意"按钮,我认为这是一个iFrame,在这site上,以获取一些数据.

picture of the popup I want to get through

我的第一个问题是我真的不知道我在处理什么.

一开始,这个页面使用CloudFlare验证码来过滤机器人,这要归功于这个video.

这就是我到目前为止try 过的:

  1. 通过其容器(类名"modfooter"和XPath //*[@id="appBody"]/div[4]/div/div/div[2]/div[2]/a[1])到达按钮
  2. 等待消息标题的出现(类名"Close")
  3. 等待消息的"X"按钮可点击(类名"Close"和XPath //*[@id="appBody"]/div[4]/div/div/div[1]/button/span)
  4. 计算页面上的iframe,这样我就可以使用带有索引参数的MOVE_TO语句

一个好的方法可能是使用EC.Frame_to_be_Available_and_Switch_to_it,但我不知道如何引用Frame.

我注意到一些可能令人感兴趣的事情:

  1. 我认为我正在处理的iframe是高度=1,宽度=1,可见性:隐藏为样式属性,它位于我试图与之交互的弹出窗口的左上角.
  2. 在iframe的标签下面是一个托管在link%的脚本

如果有任何关于如何解决这个问题或我应该指向哪个方向的建议,我将不胜感激

提前谢谢你, 安东尼奥

推荐答案

我想我能找到你所在的页面...至少它看起来是一样或相似的.我判断了同意按钮,发现它不在IFRAME中.下面的XPath将找到该按钮.

//a[text()='Agree']

它基本上只是寻找一个包含文本"同意"的A标签.


您的代码缺少等待"同意"按钮可点击的时间.您已注释掉等待,但定位器未完成,因此失败.我删除了不需要的代码,重新添加了等待,并修复了定位器.下面的代码正在运行.

#Opens 2 pages in the browser to simulate human behaviour
driver.execute_script("window.open('https://beacon.schneidercorp.com/Application.aspx?AppID=1015&LayerID=21105&PageTypeID=2&PageID=9036', '_blank')")
#Moves to beacon schneider window
driver.switch_to.window(driver.window_handles[1])
#Goes to the captcha frame
driver.switch_to.frame(0)
#Clicks on the chaptcha checkbox
driver.find_element(By.XPATH, '//*[@id="challenge-stage"]/div/label/input').click()
driver.switch_to.default_content    
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[text()='Agree']"))).click()

Python相关问答推荐

运行回文查找器代码时发生错误:[类型错误:builtin_index_or_system对象不可订阅]

如何使用Python将工作表从一个Excel工作簿复制粘贴到另一个工作簿?

重新匹配{ }中包含的文本,其中文本可能包含{{var}

在线条上绘制表面

如何从在虚拟Python环境中运行的脚本中运行需要宿主Python环境的Shell脚本?

如何将一个动态分配的C数组转换为Numpy数组,并在C扩展模块中返回给Python

如果值发生变化,则列上的极性累积和

如何设置视频语言时上传到YouTube与Python API客户端

当我try 在django中更新模型时,模型表单数据不可见

字符串合并语法在哪里记录

为什么Django管理页面和我的页面的其他CSS文件和图片都找不到?'

如何在TensorFlow中分类多个类

如何在达到end_time时自动将状态字段从1更改为0

在输入行运行时停止代码

在方法中设置属性值时,如何处理语句不可达[Unreacable]";的问题?

如何在Python Pandas中填充外部连接后的列中填充DDL值

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

无法在Spyder上的Pandas中将本地CSV转换为数据帧

Pandas数据框上的滚动平均值,其中平均值的中心基于另一数据框的时间

类型对象';敌人';没有属性';损害';