已经感谢为我提供帮助的用户。我正在导航到网址enfa.co.uk。我将ChromeDriver与Python中的Selenium结合使用,并导航(通过iframe)从左侧菜单中单击“俱乐部”链接,然后切换iframe以选择下拉值“选择俱乐部=什鲁斯伯里镇”,然后单击特定的赛季“ 1950/51”。
这将我带到一个表,该表试图单击“ Res”列下的值。这将带我进入更多数据(我必须登录才能访问此数据,但是无论哪种方式,用户单击此链接都会将其带到也具有所需结果的订阅页面)。
我已经通过计算行数/列数来检查我是否在期望表中,这与我的期望相符(97/9)。但是,当我尝试单击所需的元素时(在此示例中,Res为0-0,应与第7行/第7列相对应),我收到一条回溯消息,指出该元素不可交互。同样,如果我尝试打印此元素,它将显示为空白。有什么想法吗?预先感谢。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.select import Select
#utilise chrome driver to open specified webpage
driver = webdriver.Chrome("/Users/philthomas/Desktop/web/chromedriver")
driver.maximize_window()
driver.get("http:enfa.co.uk")
#switch to specific iframe and click on 'clubs' button on left hand menu
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"left")))
ClubsLink = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),' Clubs')]")))
ClubsLink.click()
#return from iframe
driver.switch_to.default_content()
#Switch to main iframe
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"main")))
#find drop-down menu and choose 'Team'
teamselect=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"team")))
select_box = Select(teamselect)
select_box.select_by_visible_text("Shrewsbury Town")
#ChooseSeason
season=WebDriverWait(driver,10)until(EC.presence_of_element_located((By.XPATH,"//a[contains(text(),'1950/51')]")))
season.click()
#count number of rows & columns in table to check:
rows=len(driver.find_elements_by_xpath("/html/body/form/div[7]/table/tbody/tr"))
cols=len(driver.find_elements_by_xpath("/html/body/form/div[7]/table/tbody/tr[7]/td"))
print('No of rows in table:', rows)
print('No of columns in table:', cols)
#click on specific Res - in this case, 0-0 Aug 19th v Scunthorpe
match=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"/html/body/form/div[7]/table/tbody/tr[7]/td[7]/a")))
match.click()
print('Check:', value)
必需元素的HTML:
跟踪错误:
答案 0 :(得分:1)
该元素在可见之前就存在于html中,因此EC.presence_of_element_located
为true,但该元素仍处于隐藏状态,无法单击。请改用EC.visibility_of_element_located
match = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "/html/body/form/div[7]/table/tbody/tr[7]/td[7]/a")))
答案 1 :(得分:0)
请找到更新的解决方案。单击元素时,需要更改显式等待方法。
我还更新了代码,以切换到默认内容,然后再切换到主框架
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support.select import Select
#utilise chrome driver to open specified webpage
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
driver.get("http:enfa.co.uk")
#switch to specific iframe and click on 'clubs' button on left hand menu
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"left")))
ClubsLink=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Clubs')]")))
ClubsLink.click()
driver.switch_to.default_content()
#Switch to main iframe
WebDriverWait(driver,15).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"main")))
#find drop-down menu and choose 'Team'
teamselect=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"team")))
select_box = Select(teamselect)
select_box.select_by_visible_text("Shrewsbury Town")
season=WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//a[contains(text(),'1950/51')]")))
season.click()
#count number of rows & columns in table to check:
rows=len(driver.find_elements_by_xpath("/html/body/form/div[7]/table/tbody/tr"))
cols=len(driver.find_elements_by_xpath("/html/body/form/div[7]/table/tbody/tr[7]/td"))
print('No of rows in table:', rows)
print('No of columns in table:', cols)
#click on specific Res - in this case, 0-0 Aug 19th v Scunthorpe
match=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(text(),'Scunthorpe & Lindsey Utd')]")))
match.click()