如果您想了解机械化python单击按钮的相关知识,那么本文是一篇不可错过的文章,我们将对python机械进行全面详尽的解释,并且为您提供关于ElementNotVisibleException:消息:
如果您想了解机械化python单击按钮的相关知识,那么本文是一篇不可错过的文章,我们将对python 机械进行全面详尽的解释,并且为您提供关于ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误、php-Python:使用机械化将数据提交到表单后,提取.csv结果、python selenium单击按钮、Python selenium无法单击按钮弹出窗口的有价值的信息。
本文目录一览:- 机械化python单击按钮(python 机械)
- ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误
- php-Python:使用机械化将数据提交到表单后,提取.csv结果
- python selenium单击按钮
- Python selenium无法单击按钮弹出窗口
机械化python单击按钮(python 机械)
我有一个带有<input type="button" name="submit" />
按钮的表单,希望能够单击它。
我已经尝试过,mech.form.click("submit")
但是会出现以下错误:
ControlNotFoundError: no control matching kind ''clickable'', id ''submit''
mech.submit()
也不起作用,因为它的类型是按钮并且不提交。
有任何想法吗?谢谢。
答案1
小编典典单击type="button"
在 纯HTML 的形式什么都不做。为了执行任何操作,必须包含 javascript 。
并且mechanize
不运行 javascript 。
因此,您的选择是:
- 自己阅读JavaScript并模拟
mechanize
其效果 - 使用
spidermonkey
运行JavaScript代码
我会做第一个,因为使用起来spidermonkey
似乎很困难,可能不值得。
ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误
我有一个包含源代码的页面,如下面的代码。在执行操作后,在其中显示“撤消”和“关闭”按钮。我试图单击“关闭”按钮。我已经尝试了下面的所有三个代码块,但都无法正常工作。有人可以指出我做错了什么,还是建议其他尝试?
html来源:
<div><div><i></i><div>Your stuff is going to <span>place</span> is on its way.</div><div><button> Undo</button></div><div><button> Close</button></div></div></div>
代码尝试:
#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()driver.find_element_by_css_selector(''.c-button.c-button--blue'').click()#driver.find_element_by_link_text(''Close'').click()
错误:
---------------------------------------------------------------------------ElementNotVisibleException Traceback (most recent call last)<ipython-input-15-6d570be770d7> in <module>() 1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click()----> 2 driver.find_element_by_css_selector(''.c-button.c-button--blue'').click() 3 #driver.find_element_by_link_text(''Close'').click()~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self) 78 def click(self): 79 """Clicks the element."""---> 80 self._execute(Command.CLICK_ELEMENT) 81 82 def submit(self):~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params) 626 params = {} 627 params[''id''] = self._id--> 628 return self._parent.execute(command, params) 629 630 def find_element(self, by=By.ID, value=None):~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params) 318 response = self.command_executor.execute(driver_command, params) 319 if response:--> 320 self.error_handler.check_response(response) 321 response[''value''] = self._unwrap_value( 322 response.get(''value'', None))~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response) 240 alert_text = value[''alert''].get(''text'') 241 raise exception_class(message, screen, stacktrace, alert_text)--> 242 raise exception_class(message, screen, stacktrace) 243 244 def _value_or_default(self, obj, key, default):ElementNotVisibleException: Message: element not interactable (Session info: chrome=72.0.3626.109) (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)
答案1
小编典典与文本的元素 关闭 是一个动态的元素,以便找到你要引起元素 WebDriverWait 的 元素是可点击 ,你既可以使用以下方法解决:
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, ''action-bar'')]//button[@c-button c-button--blue'' and normalize-space()=''Close'']"))).click()
注意 :您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
php-Python:使用机械化将数据提交到表单后,提取.csv结果
我是第一次使用Python从网络上提取数据.多亏了其他一些帖子以及webpage,我才知道如何使用机械化模块将数据提交到表单.
现在,我坚持寻找如何提取结果.提交表单时会有很多不同的结果,但是如果我可以访问csv文件,那将是完美的.我假设您必须使用re模块,但是如何通过Python下载结果呢?
运行作业后,csv文件位于此处:摘要=>结果=>下载重链表(您只需单击“加载示例”即可查看该网页的工作方式).
import re
import mechanize
br = mechanize.browser()
br.set_handle_robots(False) # ignore robots
br.set_handle_refresh(False) # can sometimes hang without this
url = 'http://circe.med.uniroma1.it/proABC/index.PHP'
response = br.open(url)
br.form = list(br.forms())[1]
# Controls can be found by name
control1 = br.form.find_control("light")
# Text controls can be set as a string
br["light"] = "DIQMTQSPASLSASVGETVTITCRASGNIHNYLAWYQQKQGKSPQLLVYYTTTLADGVPSRFSGSGSGTQYSLKINSLQPEDFGSYYCQHFWSTPRTFGGGTKLEIKRADAAPTVSIFPPSSEQLTSGGASVVCFLNNFYPKDINVKWKIDGSERQNGVLNSWTDQDSKDSTYSMsstLTLTKDEYERHNSYTCEATHKTSTSPIVKSFNRNEC"
br["heavy"] = "QVQLKESGPGLVAPSQSLSITCTVSGFSLTGYGVNWVRQPPGKGLEWLGMIWGDGNTDYNSALKSRLSISKDNSKSQVFLKMNSLHTDDTARYYCARERDYRLDYWGQGTTLTVSSASTTPPSVFPLAPGSAAQTNSMVTLGCLVKGYFPEPVTVTWNSGSLSSGVHTFPAVLQSDLYTLSSSVTVPsspRPSETVTCNVahpAsstKVDKKIVPRDC"
# To submit form
response = br.submit()
content = response.read()
# print content
result = re.findall(r"Prob_Heavy.csv", content)
print result
打印内容时,我感兴趣的行如下所示:
<h2>Results</h2><br>
Predictions for Heavy Chain:
<a href='u17003I9f1/Prob_Heavy.csv'>Download Heavy Chain Table</a><br>
Predictions for Light Chain:
<a href='u17003I9f1/Prob_Light.csv'>Download Light Chain Table</a><br>
因此,问题是:如何下载/访问href =’u17003I9f1 / Prob_Heavy.csv’?
解决方法:
这是一个使用BeautifulSoup的快速且肮脏的示例,它请求避免使用正则表达式解析HTML. sudo pip install bs4(如果您已安装pip但尚未安装BeautifulSoup).
import re
import mechanize
from bs4 import BeautifulSoup as bs
import requests
import time
br = mechanize.browser()
br.set_handle_robots(False) # ignore robots
br.set_handle_refresh(False) # can sometimes hang without this
url_base = "http://circe.med.uniroma1.it/proABC/"
url_index = url_base + "index.PHP"
response = br.open(url_index)
br.form = list(br.forms())[1]
# Controls can be found by name
control1 = br.form.find_control("light")
# Text controls can be set as a string
br["light"] = "DIQMTQSPASLSASVGETVTITCRASGNIHNYLAWYQQKQGKSPQLLVYYTTTLADGVPSRFSGSGSGTQYSLKINSLQPEDFGSYYCQHFWSTPRTFGGGTKLEIKRADAAPTVSIFPPSSEQLTSGGASVVCFLNNFYPKDINVKWKIDGSERQNGVLNSWTDQDSKDSTYSMsstLTLTKDEYERHNSYTCEATHKTSTSPIVKSFNRNEC"
br["heavy"] = "QVQLKESGPGLVAPSQSLSITCTVSGFSLTGYGVNWVRQPPGKGLEWLGMIWGDGNTDYNSALKSRLSISKDNSKSQVFLKMNSLHTDDTARYYCARERDYRLDYWGQGTTLTVSSASTTPPSVFPLAPGSAAQTNSMVTLGCLVKGYFPEPVTVTWNSGSLSSGVHTFPAVLQSDLYTLSSSVTVPsspRPSETVTCNVahpAsstKVDKKIVPRDC"
# To submit form
response = br.submit()
content = response.read()
# print content
soup = bs(content)
urls_csv = [x.get("href") for x in soup.findAll("a") if ".csv" in x.get("href")]
for file_path in urls_csv:
status_code = 404
retries = 0
url_csv = url_base + file_path
file_name = url_csv.split("/")[-1]
while status_code == 404 and retries < 10:
print "{} not ready yet".format(file_name)
req = requests.get(url_csv )
status_code = req.status_code
time.sleep(5)
print "{} ready. Saving.".format(file_name)
with open(file_name, "wb") as f:
f.write(req.content)
在REPL中运行脚本:
Prob_Heavy.csv not ready yet
Prob_Heavy.csv not ready yet
Prob_Heavy.csv not ready yet
Prob_Heavy.csv ready. Saving.
Prob_Light.csv not ready yet
Prob_Light.csv ready. Saving.
>>>
>>>
python selenium单击按钮
我是python selenium的新手,我尝试单击具有以下html结构的按钮:
<div> <divonclick="submitForm(''mTF'')"> <inputtype="button"></input> <div></div> <span> Search </span> </div> <divonclick="submitForm(''rMTF'')"> <inputtype="button"></input> <span> Reset </span> </div></div>
我希望能够同时单击上方的Search
和Reset
按钮(显然是单独单击)。
我尝试了几件事,例如:
driver.find_element_by_css_selector(''.button .c_button .s_button'').click()
要么,
driver.find_element_by_name(''s_image'').click()
要么,
driver.find_element_by_class_name(''s_image'').click()
但是,我似乎总是以结尾NoSuchElementException
,例如:
selenium.common.exceptions.NoSuchElementException: Message: u''Unable to locate element: {"method":"name","selector":"s_image"}'' ;
我想知道是否可以以某种方式使用HTML的onclick属性来进行selenium单击?
任何能将我指向正确方向的想法都很棒。谢谢。
答案1
小编典典对于python,请使用
from selenium.webdriver import ActionChains
和
ActionChains(browser).click(element).perform()
Python selenium无法单击按钮弹出窗口
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
import time
driver = webdriver.Chrome()
driver.get('https://developers.google.com/gmail/api/quickstart/python')
elem=driver.find_element_by_xpath(
"//a[@]")
driver.execute_script("arguments[0].scrollIntoView({block: 'center'})",elem)
elem.click()
wait = WebDriverWait(driver,50)
wait.until(EC.frame_to_be_available_and_switch_to_it(
(By.CSS_SELECTOR,'.devsite-henhouse-dialog iframe')))
wait.until(EC.presence_of_element_located(
(By.XPATH,"//button[@]"))).click()
它在 iframe 内,将其切换到它并使用 webdriver 等待
关于机械化python单击按钮和python 机械的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于ElementNotVisibleException:消息:尝试通过Selenium和Python单击按钮时,元素不可交互错误、php-Python:使用机械化将数据提交到表单后,提取.csv结果、python selenium单击按钮、Python selenium无法单击按钮弹出窗口等相关知识的信息别忘了在本站进行查找喔。
本文标签: