删除 css 选择器中类之间的空格:
driver.find_element_by_css_selector('.button .c_button .s_button').click()
# ^ ^
=>
driver.find_element_by_css_selector('.button.c_button.s_button').click()
对于Python,使用
from selenium.webdriver import ActionChains
和
ActionChains(browser).click(element).perform()
打开一个网站 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()
我在使用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 尚未执行。
我认为你的选择:
xpath2 =“你要点击的xpath”
WebDriverWait(驱动程序,超时=5).until(lambda x: x.find_element_by_xpath(xpath2))
xpath 方法不一定更好,我只是更喜欢它,您也可以使用选择器方法。
我遇到了同样的问题,并且使用 Firefox,我通过以下步骤获得了按钮元素:
nodeCssSelector
并复制值这允许我通过使用接受雅虎网站的cookie。
url = "https://yahoo.com"
driver = Firefox(executable_path="geckodriver.exe")
driver.get(url)
driver.find_element_by_css_selector("button.btn:nth-child(5)").click()
我对此进行了进一步测试,它使我能够轻松接受单独的 cookie。只需重复之前提到的步骤即可获取按钮名称。
url = "https://yahoo.com"
driver = Firefox(executable_path="geckodriver.exe")
driver.get(url)
driver.find_element_by_css_selector("a.btn").click()
driver.find_element_by_css_selector(".firstPartyAds > div:nth-child(2) > label:nth-child(1)").click()
driver.find_element_by_css_selector(".preciseGeolocation > div:nth-child(2) > label:nth-child(1)").click()
driver.find_element_by_css_selector("button.btn").click()
另一种方法是
但是,我认为第二种方法可能包含空格,具体取决于您复制的内容,因此您可能需要手动删除(其中一些)它们。第一种方法似乎更万无一失,但我不知道它是否/如何在火狐以外的其他浏览器上工作。第二种方法应该适用于所有浏览器。
e = driver.find_element(By.XPATH, 's_image').click()
有时它不起作用! 你可以尝试:
e = driver.find_element(By.XPATH, 's_image') driver.execute_script("arguments[0].click();", e)
使用此代码点击按钮
# finding the button using ID
button = driver.find_element_by_id(ID)
# clicking on the button
button.click()
对于Python,你可以做类似的事情
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
button = driver.find_element(By.ID, "my_button")
element.send_keys(Keys.RETURN)
这样做的原因是,当您使用
webdriver.find_element
时,给定的元素将被聚焦,并且当按钮被聚焦时,按“Enter”键将触发该按钮上的单击事件。