AJW*_*AJW 65 python selenium click onclick selenium-webdriver
我是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>
<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>
Run Code Online (Sandbox Code Playgroud)
我希望能够点击上面的Search
和Reset
按钮(显然是单独的).
我尝试了几件事,例如:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
Run Code Online (Sandbox Code Playgroud)
要么,
driver.find_element_by_name('s_image').click()
Run Code Online (Sandbox Code Playgroud)
要么,
driver.find_element_by_class_name('s_image').click()
Run Code Online (Sandbox Code Playgroud)
但是,我似乎总是最终得到NoSuchElementException
,例如:
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to locate element: {"method":"name","selector":"s_image"}' ;
Run Code Online (Sandbox Code Playgroud)
我想知道我是否能以某种方式使用HTML的onclick属性来进行selenium点击?
任何可以指向正确方向的想法都会很棒.谢谢.
fal*_*tru 82
删除css选择器中类之间的空格:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
# ^ ^
Run Code Online (Sandbox Code Playgroud)
=>
driver.find_element_by_css_selector('.button.c_button.s_button').click()
Run Code Online (Sandbox Code Playgroud)
小智 27
对于 python,使用
from selenium.webdriver import ActionChains
Run Code Online (Sandbox Code Playgroud)
和
ActionChains(browser).click(element).perform()
Run Code Online (Sandbox Code Playgroud)
Car*_*585 22
试试这个:
下载firefox,添加插件"firebug"和"firepath"; 安装完成后,转到您的网页,启动firebug并找到元素的xpath,它在页面中是唯一的,这样您就不会犯任何错误.
browser.find_element_by_xpath('只需复制并粘贴Xpath').click()
我在使用 Phantomjs 作为浏览器时遇到了同样的问题,所以我通过以下方式解决了:
driver.find_element_by_css_selector('div.button.c_button.s_button').click()
Run Code Online (Sandbox Code Playgroud)
基本上我已经将 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'))
Run Code Online (Sandbox Code Playgroud)
然后,您应该有两个文本文件,其中包含您所在的初始页面(“output_init.txt”)和单击按钮后转发到的页面(“output_dest.txt”)。如果它们相同,那么是的,您的代码不起作用。如果不是,那么您的代码可以工作,但您还有另一个问题。对我来说,问题似乎是转换内容以生成我的钩子所需的 JavaScript 尚未执行。
我认为你的选择:
xpath2 =“您要点击的xpath”
WebDriverWait(驱动程序,超时=5).until(lambda x: x.find_element_by_xpath(xpath2))
xpath 方法不一定更好,我只是更喜欢它,您也可以使用选择器方法。
打开一个网站https://adviserinfo.sec.gov/compilation并单击按钮下载文件,如果它使用 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()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
182136 次 |
最近记录: |