python selenium单击按钮
我是python selenium的新手,我尝试单击具有以下html结构的按钮:python selenium单击按钮,python,selenium,selenium-webdriver,onclick,click,Python,Selenium,Selenium Webdriver,Onclick,Click,我是python selenium的新手,我尝试单击具有以下html结构的按钮: <div class="b_div"> <div class="button c_button s_button" onclick="submitForm('mTF')"> <input class="very_small" type="button"></input> <div class="s_image">&
<div class="b_div">
<div class="button c_button s_button" onclick="submitForm('mTF')">
<input class="very_small" type="button"></input>
<div class="s_image"></div>
<span>
Search
</span>
</div>
<div class="button c_button s_button" onclick="submitForm('rMTF')" style="margin-bottom: 30px;">
<input class="v_small" type="button"></input>
<span>
Reset
</span>
</div>
</div>
或者
或者
但是,我似乎总是以NoTouchElementException
结束,例如:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;
我想知道我是否可以使用HTML的onclick属性使selenium单击
任何能为我指明正确方向的想法都会很好。
谢谢。删除css选择器中类之间的空格:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
# ^ ^
=>
试试这个:
下载firefox,添加插件“firebug”和“firepath”;安装后,转到您的网页,启动firebug并找到元素的xpath,它在页面中是唯一的,这样您就不会犯任何错误
见图:
browser。通过xpath(“只需复制并粘贴xpath”)查找元素。单击()
我在使用Phantomjs作为浏览器时遇到了同样的问题,因此我用以下方法解决了这个问题:
driver.find_element_by_css_selector('div.button.c_button.s_button').click()
实际上,我已经将DIV标记的名称添加到了引号中。以下调试过程帮助我解决了类似的问题
with open("output_init.txt", "w") as text_file:
text_file.write(driver.page_source.encode('ascii','ignore'))
xpath1 = "the xpath of the link you want to click on"
destination_page_link = driver.find_element_by_xpath(xpath1)
destination_page_link.click()
with open("output_dest.txt", "w") as text_file:
text_file.write(driver.page_source.encode('ascii','ignore'))
然后,您应该有两个文本文件,其中包含您所在的初始页面(“output_init.txt”)和单击按钮后转发到的页面(“output_dest.txt”)。如果它们是一样的,那么是的,你的代码不起作用。如果没有,那么代码工作正常,但您还有另一个问题。
对我来说,问题似乎是转换内容以生成钩子的必要javascript尚未执行
在我看来,你的选择是:
from selenium.webdriver import ActionChains
及
打开一个网站,点击按钮下载文件,甚至我想关闭使用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
from selenium.webdriver.chrome.options import Options
#For Mac - If you use windows change the chromedriver location
chrome_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(chrome_path)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-popup-blocking")
driver.maximize_window()
driver.get("https://adviserinfo.sec.gov/compilation")
# driver.get("https://adviserinfo.sec.gov/")
# tabName = driver.find_element_by_link_text("Investment Adviser Data")
# tabName.click()
time.sleep(3)
# report1 = driver.find_element_by_xpath("//div[@class='compilation-container ng-scope layout-column flex']//div[1]//div[1]//div[1]//div[2]//button[1]")
report1 = driver.find_element_by_xpath("//button[@analytics-label='IAPD - SEC Investment Adviser Report (GZIP)']")
# print(report1)
report1.click()
time.sleep(5)
driver.close()
我已经试过你的建议了。我得到了相同的
NoTouchElementException
错误@AJW,请尝试打印(driver.page\u source),并检查html是否实际包含该元素。谢谢。我打印了(driver.page\u source),发现它的名字不一样。奇怪。现在,当我把空格拿走并重新命名时,它发出咔哒声。下面是tho:正如您所看到的,即使是重置按钮和搜索按钮也有相同的类:在这种情况下,单击时如何区分搜索按钮和重置按钮?@AJW,aobut如何使用xpath:驱动程序。通过xpath('.//div[@class=“button c_button s_button”][包含(,“search”)]]查找元素
@MortezaLSC,若你们的意思是在并没有GUI的系统中,这是可能的。使用无头浏览器。例如,PhantomJS。非常感谢您提供如此棒的lifehack。它节省了很多小时这在mac bc上不起作用firebug和fire path都没有显示为附加组件有些时候这不是操作系统的问题,但是Firefox版本,上一个Firefox版本的FirePath有一些问题,我使用的是Firefox 55.0.3你可以使用以下方法在Firefox上找到元素:工具->Web开发者->检查器;单击GUI上的按钮,在inspector部分,用鼠标右键单击相关代码->复制并选择:CSS选择器/CSS路径/Xpath…节省了我的时间。。。谢谢你,伙计。。现在,您不需要firebug-only xPath来完成查找xPath插件的全部工作:什么是元素
?@rosefun元素
可以指:我的第一个标题
,我的第一段。
,提交
,或者开始标记和结束标记之间的任何内容
with open("output_init.txt", "w") as text_file:
text_file.write(driver.page_source.encode('ascii','ignore'))
xpath1 = "the xpath of the link you want to click on"
destination_page_link = driver.find_element_by_xpath(xpath1)
destination_page_link.click()
with open("output_dest.txt", "w") as text_file:
text_file.write(driver.page_source.encode('ascii','ignore'))
from selenium.webdriver import ActionChains
ActionChains(browser).click(element).perform()
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
from selenium.webdriver.chrome.options import Options
#For Mac - If you use windows change the chromedriver location
chrome_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(chrome_path)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-popup-blocking")
driver.maximize_window()
driver.get("https://adviserinfo.sec.gov/compilation")
# driver.get("https://adviserinfo.sec.gov/")
# tabName = driver.find_element_by_link_text("Investment Adviser Data")
# tabName.click()
time.sleep(3)
# report1 = driver.find_element_by_xpath("//div[@class='compilation-container ng-scope layout-column flex']//div[1]//div[1]//div[1]//div[2]//button[1]")
report1 = driver.find_element_by_xpath("//button[@analytics-label='IAPD - SEC Investment Adviser Report (GZIP)']")
# print(report1)
report1.click()
time.sleep(5)
driver.close()