简体   繁体   English

Selenium - 返回 xpath 中的所有文本

[英]Selenium - returning all text within an xpath

I have the below code which is supposed to return all the text within a given xpath.我有下面的代码,它应该返回给定 xpath 中的所有文本。 This xpath corresponds to the first row of EPG data for the channel NPO 1, however I am receiving nothing back:这个 xpath 对应于频道 NPO 1 的第一行 EPG 数据,但是我没有收到任何回复:

import sys
import os
import os.path
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.proxy import Proxy, ProxyType

sys.path.append("G:\\Python36\\mypath")

chrome_options = Options()  
chrome_options.add_argument("--headless")

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("https://tvepg.eu/en/netherlands/epg/main")

link_path = '/html/body/table/tbody/tr[215]/td[2]'

link_path2 = driver.find_elements_by_xpath(link_path)

for link in link_path2:
   
    print link.text
    
driver.quit()    

...any ideas as to what I need to change? ...关于我需要改变什么的任何想法?

Thanks谢谢

I am not sure whether you are after this or not.我不确定你是否在这之后。

Induce WebDriverWait () and wait for visibility_of_all_elements_located () and use the following xpath诱导WebDriverWait () 并等待visibility_of_all_elements_located () 并使用以下 xpath

from selenium import webdriver
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("https://tvepg.eu/en/netherlands/epg/main")
elements=WebDriverWait(driver,10).until(EC.visibility_of_all_elements_located((By.XPATH,"//table[@class='grid-container main-table']/tbody//tr[1]/td[1]//img[@class='card-img-topa']")))
for element in elements:
    print(element.get_attribute("alt"))

Output : Output

NPO 1
NPO 2
NPO 1 extra
NPO 2 extra
RTL 4
RTL 5
SBS6
RTL 7
Net5
RTL 8
SBS9
RTL Lounge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM