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
如何使用SeleniumWebDriver和Python提取元素中的文本?_Python_Selenium_Selenium Webdriver_Xpath_Webdriverwait - Fatal编程技术网

如何使用SeleniumWebDriver和Python提取元素中的文本?

如何使用SeleniumWebDriver和Python提取元素中的文本?,python,selenium,selenium-webdriver,xpath,webdriverwait,Python,Selenium,Selenium Webdriver,Xpath,Webdriverwait,抓取指定区域的文本 网站: 图片: 代码: HTML: 大塊文化 请尝试以下代码 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.get(

抓取指定区域的文本

网站:

图片:

代码:

HTML:

大塊文化

请尝试以下代码

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.get("https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ")
element=WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'a.description-anchor span[translate="no"]')))
print(element.text)
你也可以使用

驱动程序。通过css选择器('span[translate=“no”]”)查找元素

CSS选择器应该比XPath更快


编辑根据DebanjanB评论编辑-谢谢你

你做得不对:

BookTitle[0]。getWindowHandle()
不希望在此处执行任何操作

只需尝试:

driver.find_element_by_css_selector("a[class='description-anchor']>span").text

要提取文本大塊文化从指定区域,您需要为位于()的元素的可见性引入WebDriverWait,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument("--disable-extensions")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ')
    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='電子書詳細資料']//following::ul[1]//li/a[@class='description-anchor']/span"))).text)
    driver.quit()
    
  • 控制台输出:

    大塊文化
    

您的选择器与元素无关,通常您可以右键单击开发工具上的元素复制选择器,或者在发布答案之前测试答案?你的代码编译吗??您的解决方案是否根据OP的要求基于Python???我很抱歉重新考虑Python解决方案,我错过了编辑为Python版本a的标记是的,它确实为我编译了我在工作中使用了这种语法。。。。然而,我感到困惑,其他答案表明,如果不是相同的解决方案,那么也非常相似,我的答案怎么可能有编译错误,而不是它们的错误呢?您缺少选择器周围的单引号,即,
“…”
,它应该是
驱动程序。通过css\u选择器查找元素('span[translate=“no”]”)
编辑,我为这个愚蠢的错误道歉
a[class='description-anchor']
->
a.description-anchor
也许更规范一些
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")
options.add_argument('disable-infobars')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.kobo.com/tw/zh/ebook/NXUCYsE9cD6OWhvtdTqQQQ')
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='電子書詳細資料']//following::ul[1]//li/a[@class='description-anchor']/span"))).text)
driver.quit()
大塊文化