如何使用Selenium单击弹出模式框中的按钮 [英] How to use Selenium to click a button in a popup modal box

查看:98
本文介绍了如何使用Selenium单击弹出模式框中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python中使用Selenium从 https://www.seekingalpha.com .主页上有一个登录/立即加入"链接.我使用Selenium来单击它,然后弹出一个弹出窗口,要求使用另一个登录"按钮来要求登录信息.似乎我下面的代码可以输入我的用户名和密码,但是我尝试单击登录"按钮没有得到正确的响应(它单击了弹出框下方的广告.)

I am trying to use Selenium in Python to pull some data from https://www.seekingalpha.com. The front page has a "Sign-in/Join now" link. I used Selenium to click it, which brought up a popup asking for sign-in information with another "Sign in" button. It seems my code below can enter my username and password, but my attempt to click the "sign in" button didn't get the right response (it clicked on the ad below the popup box.)

我正在使用Python 3.5.

I am using Python 3.5.

这是我的代码:

driver = webdriver.Chrome()

url = "https://seekingalpha.com"

driver.get(url)

sleep(5)

driver.find_element_by_xpath('//*[@id ="sign-in"]').click() 

sleep(5)

driver.find_element_by_xpath('//*[@id ="authentication_login_email"]').send_keys("xxxx@gmail.com") 

driver.find_element_by_xpath('//*[@id ="authentication_login_password"]').send_keys("xxxxxxxxx") 

driver.find_element_by_xpath('//*[@id="log-btn"]').click()

任何建议/建议都将不胜感激.

Any advice/suggestion is greatly appreciated.

推荐答案

以前的答案"有误,因此我已对其进行了更新.

previous 'answer' was wrong so I have updated it.

知道了,这是您需要做的:
1.)获取最新的Firefox
2.)获取最新的geckodriver
3.)使用firefox驱动程序

Got you man, this is what you need to do:
1.) grab the latest firefox
2.) grab the latest geckodriver
3.) use a firefox driver

driver = webdriver.Firefox(executable_path=r'd:\Python_projects\geckodriver.exe')

url = "https://seekingalpha.com"

driver.get(url)

sign_in = driver.find_element_by_xpath('//*[@id ="sign-in"]')
driver.execute_script('arguments[0].click()', sign_in)
time.sleep(1)

email = driver.find_element_by_xpath('//*[@id ="authentication_login_email"]')
email.send_keys("xxxx@gmail.com")
pw = driver.find_element_by_xpath('//*[@id ="authentication_login_password"]')
pw.send_keys("xxxxxxxxx")
pw.send_keys(Keys.ENTER)

说明:

如果浏览器

It is easy to detect if selenium is used or not if the browser tells that information (and it seems this page does not want to be scraped):

导航器界面的webdriver只读属性指示用户代理是否由自动化控制.

The webdriver read-only property of the navigator interface indicates whether the user agent is controlled by automation.

我一直在寻找如何绕过检测的答案,并发现了文章.

I have looked for an answer how to bypass detection and found this article.

您最好在使用Selenium时避免检测,这将要求您使用最新版本的Firefox之一,而这似乎并没有显示出您正在使用Firefox的明显迹象.

Your best of avoiding detection when using Selenium would require you to use one of the latest builds of Firefox which don’t appear to give off any obvious sign that you are using Firefox.

开枪,启动后加载正确的页面设计,并且登录尝试的结果与手动尝试相同.

Gave a shot and after launch the correct page design loaded and the login attempt resulted the same like the manual attempt.

通过更多搜索发现,如果您修改了chromedriver ,即使有chromedriver.

Also with a bit more searching found that if you modify your chromedriver, you have a chance to bypass detection even with chromedriver.

今天也学到了一些新东西.\ o/

Learned something new today too. \o/

其他想法:

我已经使用嵌入式铬(CEF)进行了一些实验.如果您通过selenium打开chrome窗口,然后打开控制台并检查 navigator.webdriver ,结果将为 True .但是,如果您打开CEF窗口,然后对其进行远程调试,则标记将为 False .我没有检查边缘情况,但使用CEF可以解决非边缘情况.

I have made a little experiment using embedded chromium (CEF). If you open a chrome window via selenium and you open the console and check navigator.webdriver the result will be True. If you open a CEF window however and then remote debug it, the flag will be False. I did not check edge cases with it but non-edge-case scenarios should be fine with CEF.

所以您将来可能想要查看的内容:

So what you may want to check out in the future:

1.)在命令行中: pip install cefpython3
2.) git clone https://github.com/cztomczak/cefpython.git
3.)打开您的CEF项目,并在示例中找到 hello.py
4.)将启动更新为 cef.Initialize(settings = {"remote_debugging_port":9222})
5.)运行 hello.py
(这是初始的一次性设置,您可以在将来进行自定义,但最主要的是,您已经打开了调试端口的浏览器)
6.)将Chrome启动修改为:

1.) in command line: pip install cefpython3
2.) git clone https://github.com/cztomczak/cefpython.git
3.) open your CEF project and find hello.pyin the examples
4.) update the startup to cef.Initialize(settings={"remote_debugging_port":9222})
5.) run hello.py
(this was the initial, one time setup, you may customize it in the future, but the main thing is done, you have a browser with a debug port open)
6.) modify chrome startup to:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.debugger_address = "127.0.0.1:9222"
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver_executable)

7.)现在,您在浏览器中有了一个没有自动"签名的驱动程序.可能有一些缺点,例如:

7.) now you have a driver without 'automated' signature in the browser. There may be some drawbacks like:

  • CEF并不是最新版本,目前最新发布的chrome是v76,CEF是v66.
  • 某些内容"也可能无法正常工作,例如 window.Notification 在CEF中不是问题
  • CEF is not super very latest, right now the latest released chrome is v76, CEF is v66.
  • also "some stuff" may not work, like window.Notification is not a thing in CEF

这篇关于如何使用Selenium单击弹出模式框中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆