Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium can';t提取文本_Python_Selenium_Web Scraping_Xpath - Fatal编程技术网

Python selenium can';t提取文本

Python selenium can';t提取文本,python,selenium,web-scraping,xpath,Python,Selenium,Web Scraping,Xpath,我正在尝试从列表中提取文本,这是URL: 这是我的代码: 1º我发现除名(ul) 对于ul中的每个li,打印文本 ul = driver.find_element_by_xpath('//h2[2]/following-sibling::ul') li = ul.find_elements_by_tag_name('li') #print(li.text) for element in li: print(element.text) 这段代码返回空格而不是文本,我做错了什么 有

我正在尝试从列表中提取文本,这是URL:

这是我的代码:

1º我发现除名(ul)

对于ul中的每个li,打印文本

ul = driver.find_element_by_xpath('//h2[2]/following-sibling::ul') 

li = ul.find_elements_by_tag_name('li')
#print(li.text)

for element in li:
    print(element.text)
这段代码返回空格而不是文本,我做错了什么

有些人的回答是,他们说话时没有检查任何东西。 1ºxpath肯定在那里,请检查它 2º页面的html中有29个H2,而不仅仅是一个

我收到了一些人的反对票,他们说的是我想的,甚至没有检查任何东西。 我要提取的是以下文本:

•Rompecabezas-解决方案varios acertijos es una de las mecánicas centrales del juego; •públicos地区的托多市——教育区托多市的juego和apropiado地区; •拱廊——德本终点站的拱廊,位于奥门坦和朱伊戈的中世纪; •Acción-este título包括desafíos que deben Superrase Utizando habilidades como precisión、tiempo de respuesta rápido等。; •超级巨星——来自世界各地的巨星,在波德雷斯尼科斯(poderesúnicos)附近的深红之路上; •联合国朱加多——历史上的孤寂中的圣约翰岛; •多民族地方政府——在各党派中扮演不同角色的权利人的职能,是一个红色国家的一部分


您定义的
'//h2[2]/以下同级::ul


这就是为什么
ul
实际上是一个
null
,而
li
是一个空列表。

1我已经设法从我所在地区获得了数据,但我不得不花一些时间从国家列表中选择西班牙。 请注意,我避免使用xpath定位器,因为它们太长了。 所有的解释都在代码的注释中

2从元素中获取文本。通常使用文本,但在这种情况下不起作用。所以
get\u属性(“innerHTML”)
才是您真正需要的

我留下了我用过的所有等待。您可以自己调试并删除不必要的

解决方案:

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

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://www.eneba.com/es/lego-dimensions-starter-pack-playstation-4')

wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "._1FArM6>.qGNWom.qGNWom"))).click()  # accept cookies
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".EcNujK._2afX4x._1OhNBA"))).click()  # click region button
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#region .css-1hwfws3"))).click()  # change region

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#region .react-select__input>input")))
driver.find_element_by_css_selector("#region .react-select__input>input").send_keys("spa")  # input country name
driver.find_element_by_css_selector("#react-select-2-option-5").click()  # select found country
driver.find_element_by_css_selector("._3Fpvn5>button[type='submit']").click()  # submit country
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div[itemprop=description]>ul>li")))  # wait for all li elements
cards = driver.find_elements_by_css_selector("div[itemprop=description]>ul>li")
for card in cards:
    print(card.get_attribute("innerHTML"))
driver.close()
driver.quit()
• Rompecabezas - resolver varios acertijos es una de las mecánicas centrales del juego;
• Para todos los públicos – El juego es apropiado para jugadores de todas las edades;
• Arcade - los jugadores deben terminar con éxito los niveles que aumentan en dificultad a medida que avanzan en el juego;
• Acción - este título incluye desafíos que deben superarse utilizando habilidades como precisión, tiempo de respuesta rápido, etc.;
• Superhéroes - Los jugadores entran en un mundo peligroso, donde los únicos capaces de detener el crimen son los héroes bendecidos con poderes únicos;
• Un jugador - el juego presenta una campaña en solitario con una historia;
• Multijugador local - esta función permite que varias personas participen en los mismos partidos, ya sea a través de la pantalla dividida o la misma conexión de red.
结果:

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

driver = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://www.eneba.com/es/lego-dimensions-starter-pack-playstation-4')

wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "._1FArM6>.qGNWom.qGNWom"))).click()  # accept cookies
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".EcNujK._2afX4x._1OhNBA"))).click()  # click region button
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#region .css-1hwfws3"))).click()  # change region

wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#region .react-select__input>input")))
driver.find_element_by_css_selector("#region .react-select__input>input").send_keys("spa")  # input country name
driver.find_element_by_css_selector("#react-select-2-option-5").click()  # select found country
driver.find_element_by_css_selector("._3Fpvn5>button[type='submit']").click()  # submit country
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "div[itemprop=description]>ul>li")))  # wait for all li elements
cards = driver.find_elements_by_css_selector("div[itemprop=description]>ul>li")
for card in cards:
    print(card.get_attribute("innerHTML"))
driver.close()
driver.quit()
• Rompecabezas - resolver varios acertijos es una de las mecánicas centrales del juego;
• Para todos los públicos – El juego es apropiado para jugadores de todas las edades;
• Arcade - los jugadores deben terminar con éxito los niveles que aumentan en dificultad a medida que avanzan en el juego;
• Acción - este título incluye desafíos que deben superarse utilizando habilidades como precisión, tiempo de respuesta rápido, etc.;
• Superhéroes - Los jugadores entran en un mundo peligroso, donde los únicos capaces de detener el crimen son los héroes bendecidos con poderes únicos;
• Un jugador - el juego presenta una campaña en solitario con una historia;
• Multijugador local - esta función permite que varias personas participen en los mismos partidos, ya sea a través de la pantalla dividida o la misma conexión de red.

我也这么想,但是为什么它不会在第一行出现
ElementNotFoundException
时失败呢?但是xpath肯定是错误的,页面上只有一个
h2
标记,因此
h2[2]
不可能是正确的。这里也是一样,如果是Java或JavaScript,我会知道它应该如何运行。如果你用Control+F检查和搜索xpath,你会找到它。就在那里。你是怎么查的??html代码中有29个H2。@Vcash我认为您的问题没有正确表述。首先,您应该指定要查找的元素,其次应该向其中添加html代码。该网站的某些内容在某些国家/地区被屏蔽。例如,我看到这样的信息:“Aún no estádisposible en tu región.”@V-cash对不起,我想我知道为什么会发生这种情况——我们看到的页面与您的不同。我之前没有仔细看,只是在控制台中搜索,但回到页面,我注意到上面写着“一个没有争议的区域”。你想得到哪一个文本?游戏标题?你想从url中提取什么??请在有输出的帖子中添加,我们看到的页面与您不同。我刚刚注意到,它向我传达了一个信息“一个没有争议的地区”——我不太流利,但我想这意味着它在美国不可用。所以这里的大多数用户看不到你的内容。好吧,现在你的问题看起来好多了。你能在屏幕截图上显示你要找的文本吗?我认为它在我所在的地区是不可见的。谢谢,我使用了.get_attirbute(“innerHTML”)而不是.text,它可以工作。