รับค่าข้อความของแท็ก HTML ผ่าน Selenium Web Automation ใน Python หรือไม่

ฉันกำลังสร้างบอท Reddit ที่จะค้นหาคุณลักษณะบางอย่างในความคิดเห็น ใช้ซีลีเนียมเพื่อเยี่ยมชมเว็บไซต์ข้อมูล และใช้ driver.find_element_by... เพื่อรับค่าภายในแท็กนั้น แต่มันไม่ทำงาน

เมื่อฉันใช้ driver.find_element_by_class_name() นี่คือข้อมูลที่ส่งคืน:

<selenium.webdriver.remote.webelement.WebElement (session="f454dcf92728b9db4de080a27a844bf7", element="514bd57d-99d7-4fce-a05d-3fa92f66ad49")>

เมื่อฉันใช้ driver.find_elements_by_css_selector(".style-scope.ytd-video-renderer") สิ่งนี้จะถูกส่งคืน:

[
  <selenium.webdriver.remote.webelement.WebElement (session="43cb953cde81df270260bf769fe081a2", element="6b4ee3e2-5e6b-48e2-8ec8-9083bf15baea")>, 
  <selenium.webdriver.remote.webelement.WebElement (session="43cb953cde81df270260bf769fe081a2", ...
]

เมื่อฉันใช้ driver.find_elements_by_css_selector(".style-scope.ytd-video-renderer")

สมมติว่านี่คือสิ่งที่ฉันพยายามค้นหา (โค้ดด้านบนส่งคืนข้อมูลซีลีเนียมด้านบนสำหรับแท็กนี้):

<yt-formatted-string class="style-scope ytd-video-renderer" aria-label="Sword Art Online: Alicization Lycoris Opening Full『ReoNa - Scar/let』 by Melodic Star 2 months ago 4 minutes, 18 seconds 837,676 views">Sword Art Online: Alicization Lycoris Opening Full『ReoNa - Scar/let』</yt-formatted-string>

สิ่งที่ฉันต้องการ

ฉันต้องการ Sword Art Online: Alicization Lycoris Opening Full『ReoNa - Scar/let』 คืน

ฉันจะทำอย่างไร?


person KazutoKiritoKirigaya    schedule 26.09.2020    source แหล่งที่มา


คำตอบ (2)


ดูเหมือนว่าคุณจะอยู่ใกล้พอ เมื่อคุณใช้ driver.find_element_by_class_name() WebElement ที่ตรงกันครั้งแรก ถูกส่งกลับ ในการพิมพ์แบบเดียวกันผลลัพธ์คือ:

<selenium.webdriver.remote.webelement.WebElement (session="f454dcf92728b9db4de080a27a844bf7", element="514bd57d-99d7-4fce-a05d-3fa92f66ad49")>

ซึ่งแสดงถึง WebElement เอง ซึ่งอาจมีข้อความที่ต้องการ

ในบรรทัดที่คล้ายกัน driver.find_elements_by_css_selector(".style-scope.ytd-video-renderer") ส่งคืน รายการ ของ WebElements ที่ตรงกัน และเมื่อพิมพ์สิ่งเหล่านั้น ผลลัพธ์คือ:

[
  <selenium.webdriver.remote.webelement.WebElement (session="43cb953cde81df270260bf769fe081a2", element="6b4ee3e2-5e6b-48e2-8ec8-9083bf15baea")>, 
  <selenium.webdriver.remote.webelement.WebElement (session="43cb953cde81df270260bf769fe081a2",
  ...
]

สารละลาย

วิธีแยกข้อความ Sword Art Online: Alicization Lycoris Opening Full『ReoNa - Scar/let』 จาก HTML ต่อไปนี้:

<yt-formatted-string class="style-scope ytd-video-renderer" aria-label="Sword Art Online: Alicization Lycoris Opening Full『ReoNa - Scar/let』 by Melodic Star 2 months ago 4 minutes, 18 seconds 837,676 views">Sword Art Online: Alicization Lycoris Opening Full『ReoNa - Scar/let』</yt-formatted-string>

คุณสามารถใช้Locator Strategies อย่างใดอย่างหนึ่งต่อไปนี้:

  • ใช้ css_selector และ get_attribute():

    print(driver.find_element_by_css_selector("yt-formatted-string.style-scope.ytd-video-renderer").get_attribute("innerHTML"))
    
  • การใช้แอตทริบิวต์ xpath และ ข้อความ:

    print(driver.find_element_by_xpath("//yt-formatted-string[@class='style-scope ytd-video-renderer']").text)
    

ตามหลักการแล้ว หากต้องการพิมพ์ข้อความ 3,862.76 คุณต้องกระตุ้น WebDriverWait สำหรับ visibility_of_element_located() และคุณสามารถใช้ อย่างใดอย่างหนึ่งต่อไปนี้ กลยุทธ์ตัวระบุตำแหน่ง:

  • ใช้ CSS_SELECTOR และ get_attribute():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "yt-formatted-string.style-scope.ytd-video-renderer"))).get_attribute("innerHTML"))
    
  • การใช้แอตทริบิวต์ XPATH และ ข้อความ:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//yt-formatted-string[@class='style-scope ytd-video-renderer']"))).text)
    
  • หมายเหตุ : คุณต้องเพิ่มการนำเข้าต่อไปนี้ :

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

คุณสามารถค้นหาการสนทนาที่เกี่ยวข้องได้ใน วิธีดึงข้อความของ WebElement โดยใช้ Selenium - Python


เอาท์โตร

ลิงก์ไปยังเอกสารที่เป็นประโยชน์:

person DebanjanB    schedule 26.09.2020
comment
ขอบคุณ แค่อยากให้คุณย้ำอีกสักหน่อย สมมติว่าฉันมี <span class="tags"> Lorem Ipsum </> หลายตัว และฉันใช้ driver.find_elements_by... แทน driver.find_element_by... มันจะส่งคืนข้อความเป็นสตริงยาวหนึ่งสตริงหรือจะมีบรรทัดใหม่สำหรับแต่ละแอตทริบิวต์ ( ฉันกำลังจัดเก็บข้อมูลนี้และให้มันตอบกลับความคิดเห็น Reddit ผ่าน PRAW ดังนั้นหากฉันใช้ comment.reply("tags: {}".format(tags)) มันจะรวมแท็กทั้งหมดเข้าด้วยกันเป็นสตริงเดียวโดยไม่มีช่องว่าง หรือจะให้ช่องว่างระหว่างแต่ละแท็กหรือไม่ - person KazutoKiritoKirigaya; 26.09.2020
comment
@KazutoKiritoKirigaya driver.find_elements จะส่งคืนรายการเสมอ คุณต้องวนซ้ำรายการเพื่อแยกข้อความ - person DebanjanB; 26.09.2020
comment
นั่นคือสิ่งที่บอกว่า driver.find_elements_by... ไม่สามารถทำซ้ำได้ - person KazutoKiritoKirigaya; 26.09.2020
comment
@KazutoKiritoKirigaya จริง แต่เราสามารถเสนอทางออกที่ดีที่สุดให้คุณได้เช่นกัน :) อย่าลังเลที่จะตั้งคำถามใหม่ตามความต้องการใหม่ของคุณ ผู้สนับสนุน StackOverflow ยินดีที่จะช่วยเหลือคุณ - person DebanjanB; 26.09.2020
comment
ไม่มีวิธีแก้ไขปัญหาอื่นที่ชัดเจนสำหรับปัญหานี้เพื่อที่ฉันจะได้แยกข้อความจาก driver.find_elements_by... หรือไม่ - person KazutoKiritoKirigaya; 26.09.2020
comment
@KazutoKiritoKirigaya ก็มีวิธีแก้ปัญหาสำหรับเรื่องนั้นเช่นกัน แต่เนื่องจากบริบทแตกต่างกัน ฉันจึงขอแนะนำให้คุณตั้งคำถามใหม่ เพื่อให้คำถามนี้และคำถามใหม่ทั้งคู่มีประโยชน์สำหรับผู้อ่านในอนาคต - person DebanjanB; 26.09.2020
comment
เสร็จแล้วหวังว่าคุณจะตอบที่นั่น! พบคำตอบของคุณสำหรับสิ่งนี้ค่อนข้างมีประโยชน์ - person KazutoKiritoKirigaya; 26.09.2020

ใช้ .text:

element = driver.find_element_by_xpath('//*[@id="container"]/h1/yt-formatted-string')
print(element.text)
person Stroe Andrei    schedule 26.09.2020