การแปลง Session และ Element ที่ได้จากการทำ Xpath เป็นลิ้งค์ในการทำ Selenium Webdriver

แชร์ความรู้ภาษา Python ไพทอน การเขียนโปรแกรมภาษาไพทอน

Moderators: mindphp, ผู้ดูแลกระดาน

rangsan
PHP Hero Member
PHP Hero Member
Posts: 199
Joined: 30/04/2018 9:44 am

การแปลง Session และ Element ที่ได้จากการทำ Xpath เป็นลิ้งค์ในการทำ Selenium Webdriver

Post by rangsan »

การแปลง Session และ Element ที่ได้จากการทำ Xpath เป็นลิ้งค์ในการทำ Selenium Webdriver

บทความนี้จะเป็นการทำลิ้งค์ ต่อยอดจาก บทความก่อน คลิกเพื่อดูบทความ

เมื่อเราได้ผลลัพธ์ของ Session และ Element จาก Web Browser โดยการทำ Xpath แล้วนั้นขั้นตอนต่อมาเราจะทำการแปลง Session และ Element ที่ได้มาให้เป็น Link เพื่อที่จะให้ Selenium นั้นสามารถคลิกได้

ผลลัพธ์ที่เป็น Session และ Element

Code: Select all

[<selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="8e69c9dd-2481-4b1a-b851-7fd100b62a79")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="f6f17cbf-0286-45da-ae47-fe0ecda961ce")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="1ae666fa-1fb6-49c3-b621-355090a98d5f")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="f59af0cc-071b-492e-8401-d3268e114b33")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="0b5d2c87-f243-4954-bd8b-4023cdcfed82")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="aa1f2d35-5799-4210-844f-4857fb182910")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="baea6fc8-1a95-4f06-b1a3-9518029429d3")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="fc6fcc71-0ffa-4eed-b234-96a61d30607e")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="e9d2f693-fb20-4473-87c1-9f17eaf32102")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="726129f5-7f20-490d-8449-a31efd60a2da")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="323e23cb-c18e-4284-8adb-258281d61880")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="49f0ddeb-0346-413a-8df1-1c62ecdb1b3c")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="5d3505c5-8ba8-4038-87b2-a427923e3256")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="25a7ed30-0535-413f-a2a4-512fe7d41c2a")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="dc01c7a6-7dfe-459c-83b3-80c625a29c99")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="f079098e-dac5-44cb-a712-2c83cbeb5c4c")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="b6b66792-4dc9-4195-83cd-3804a7e6af82")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="6c7b9e09-e4ec-45df-ab42-fe021f910c71")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="4fea8e98-b47e-4997-a2d7-3fcdf3cfe37e")>, <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="d7b3c63b-cf02-43ac-9dbe-58689e423336", element="99d1e501-e597-4d5a-bd9d-aa554d18ac7f")>]
เมื่อเราได้ผลลัพธ์ข้างต้นแล้วต่อมาจะเริ่มทำการแปลง Session และ Element เป็น Link โดยใช้คำสั่งนี้

Code: Select all

continue_link = driver.find_element_by_tag_name('a')
title_list = driver.find_elements_by_xpath(
    "//div[@class='category-list']//table[@class='category table table-striped table-bordered table-hover']/tbody/tr//a[@href]")
for web_link in title_list:
    link = web_link.get_attribute("href")
    print(link)
อธิบายโค้ดแต่ละส่วน
1. เป็นโค้ดที่ใช้ในการหา Tag 'a'

Code: Select all

continue_link = driver.find_element_by_tag_name('a')
2. เป็นโค้ดที่ใช้ในการทำ Xpath แต่มีเพิ่มในส่วนของ (//a[@href])

Code: Select all

title_list = driver.find_elements_by_xpath(
    "//div[@class='category-list']//table[@class='category table table-striped table-bordered table-hover']/tbody/tr//a[@href]")
3. นำ Session และ Element ที่ได้มา Loop เพื่อหาลิ้งค์

Code: Select all

for web_link in title_list:
    link = web_link.get_attribute("href")
    print(link)
ผลลัพธ์ที่ได้จากโค้ดโดยรวม

Code: Select all

https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5472-windows-camera.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5454-high-dynamic-range.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5430-flicks.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5384-tls-ssl.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5375-2fa.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5242-ip.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5219-simulation.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5173-program-and-software.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5018-computer-system-operation.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/5014-gen-computer.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4969-tax.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4946-git-command.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4825-teamviewer-lan-connections.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4824-adobe-flash-builder.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4810-creative-cloud.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4763-timing-for-work.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4750-principles-of-system-testing.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4657-onboard_gpu.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4652-gpu_1.html
https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1/31-%E0%B8%84%E0%B8%A7%E0%B8%B2%E0%B8%A1%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%97%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B9%84%E0%B8%9B/4650-checkversionfirefox.html
ภาพประกอบของผลลัพธ์
output_link.png
It’s never too late to start again.

  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Knowledge”

Who is online

Users browsing this forum: No registered users and 13 guests