2016-06-08 49 views
1

我正試圖按下這個「新搜索」按鈕。它在輸入搜索後出現在屏幕的頂部http://www.lexisnexis.com/hottopics/lnacademic/用Selenium/Firefox找不到/單擊此按鈕

我已經看過了Xpath和唯一選擇器。

我曾嘗試:

browser.find_element_by_css_selector('#restoreButtons > a:nth-child(3)').click() 
browser.find_element_by_xpath(id('restoreButtons')/x:a[3]) 
browser.find_element_by_xpath(/x:a[3]) 

對於所有三個,我得到一個「無法找到元素錯誤」

+0

你可以去'//一個[文本()=「新搜索」]' – Madhan

回答

0

這是因爲該元素是iframe內。你必須在上下文中搜索內部的元素。使用.switch_to.frame()

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 

wait = WebDriverWait(driver, 10) 
frame = wait.until(EC.presence_of_element_located((By.ID, "mainFrame"))) 
driver.switch_to.frame(frame) 

僅供參考,這裏是完整的工作代碼:

from selenium import webdriver 
from selenium.webdriver import ActionChains 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 


driver = webdriver.Chrome() 
driver.get('http://www.lexisnexis.com/hottopics/lnacademic/') 

actions = ActionChains(driver) 
wait = WebDriverWait(driver, 10) 

frame = wait.until(EC.presence_of_element_located((By.ID, "mainFrame"))) 
driver.switch_to.frame(frame) 

driver.find_element_by_id("terms").send_keys("Test") 
driver.find_element_by_id("srchButt").click() 
+0

我試過了,得到了一個錯誤,說是找不到框架 – user6437583

+0

@ user6437583好了,現在怎麼樣? – alecxe

+0

我現在正在收到timeoutexception錯誤 – user6437583

0

要建立在alecxes答案,

一旦你的iframe中,可使用查找點擊它的xpath:

new_search_xpath = '/html/body/div[2]/table/tbody/tr[2]/td[2]/div[1]/table/tbody/tr/td/span/a[3]' 
new_search = driver.find_element(By.XPATH, new_search_xpath) 
new_search.click() 

你應該考慮安裝firebug firefox插件來抓取Xpat HS: https://addons.mozilla.org/en-US/firefox/addon/firebug/

+0

我仍然收到一個timeoutexception:/ – user6437583