Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
532 views
in Technique[技术] by (71.8m points)

python - Selenium Python-可以找到表(带回行数/列数),但是找不到要单击的特定元素(Selenium Python - can locate table (bring back no. rows/cols) but can't find specific element to click)

Thanks already to the users who have helped me on this piece of work.

(在此感谢已经为我提供帮助的用户。)

I am navigating to the url enfa.co.uk.

(我正在导航到网址enfa.co.uk。)

I use ChromeDriver with Selenium in Python and navigate (via iframe) to click on the 'Clubs' link from the left hand menu, then switch iframes to select a drop-down value 'Select Club = Shrewsbury Town' and then click on a specific season '1950/51'.

(我将ChromeDriver与Python中的Selenium结合使用,并导航(通过iframe)从左侧菜单中单击“俱乐部”链接,然后切换iframe以选择下拉值“选择俱乐部=什鲁斯伯里镇”,然后单击特定的第1950/51赛季。)

This takes me to a table for which I am trying to click on a value under the column 'Res'.

(这将我带到一个表,该表试图单击“ Res”列下的值。)

This would take me to further data (I have to login to access this data, but either way, a user clicking on this link would take them to the subscribe page which also has the desired result).

(这将带我进入更多数据(我必须登录才能访问此数据,但是无论哪种方式,用户单击此链接都会将其带到也具有所需结果的订阅页面)。)

I have checked that I am in within the expected table by calculating the number of rows/columns and this matches my expectations (97/9).

(我已经通过计算行数/列数来检查自己是否在期望的表中,这与我的期望(97/9)相符。)

However when I try and click on the element required (in this example, the Res is 0-0 which should correspond to row 7 / column 7) I receive a traceback stating that the element is not interactable.

(但是,当我尝试单击所需的元素时(在此示例中,Res为0-0,应与第7行/第7列相对应),我收到一条回溯消息,指出该元素不可交互。)

Similarly if I try and print this element it shows as blank.

(同样,如果我尝试打印此元素,它将显示为空白。)

Any thoughts?

(有什么想法吗?)

Thanks in advance.

(提前致谢。)

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 of required element:

(所需元素的HTML:)

的HTML

Traceback error:

(回溯错误:)

追溯

  ask by pippo_T translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The element is present in the html before it's visible, so EC.presence_of_element_located is true but the element is still hidden and can't be clicked.

(该元素在可见之前就存在于html中,因此EC.presence_of_element_located为true,但该元素仍处于隐藏状态,无法单击。)

Use EC.visibility_of_element_located instead

(使用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")))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...