การเขียน selenium webdriver ด้วยภาษา python

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การเขียน selenium webdriver ด้วยภาษา python

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย Forexduck » 02/06/2022 11:36 pm

โห แอดมินเก่งจังเลยครับ

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย mindphp » 17/09/2017 6:14 am

ปุ่มชื่อ โพสต์ ใช้ xpath ตามนี้ดู

โค้ด: เลือกทั้งหมด

browser.find_element_by_xpath("//input[@value='โพสต์']").click();
ส่วนถ้าจะใช้ is_element_present สร้างฟังก์ชั่น ไว้

โค้ด: เลือกทั้งหมด

def is_element_present(how, what):
    try: 
	browser.find_element(by=how, value=what)
    except NoSuchElementException: 
	return False
    return True
คำสั่งอื่นๆ เกี่ยวกับ Wait For Element ลองดูที่
http://selenium-python.readthedocs.io/waits.html

ตัวอย่าง การคลิกปุ่มที่เป็น #Ajax ที่ควรรอ ก่อนกด เช่น

โค้ด: เลือกทั้งหมด

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

browser = webdriver.Chrome('D:\\tmp\\chromedriver.exe')

browser.get('https://www.mindphp.com/forums/ucp.php?mode=login')

user = browser.find_element_by_css_selector('#username')
user.send_keys('ชื่อ User')
password = browser.find_element_by_css_selector('#password')
password.send_keys('รหัสผ่าน')
login = browser.find_element_by_name('login')
login.click()

# กดปุ่ม like ใน post ที่ต้องการ 
browser.get('https://www.mindphp.com/forums/viewtopic.php?f=16&t=42465&start=8')
wait = WebDriverWait(browser, 5)
wait.until(EC.visibility_of_element_located((By.ID, "like_img_104452")))
browser.find_element_by_id("like_img_104452").click()

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย natnicha001 » 16/09/2017 5:26 pm

หมายความอย่างนี้หรอคะ TOT"

โค้ด: เลือกทั้งหมด

try:
    time.sleep(5)
    driver.is_element_present(By.NAME, "post")
except NoSuchElementException:
    print ('กรุณาตรวจสอบสอบ')
erro

โค้ด: เลือกทั้งหมด

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 43, in <module>
    driver.is_element_present(By.NAME, "post")
AttributeError: 'WebDriver' object has no attribute 'is_element_present'

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย thatsawan » 16/09/2017 4:43 pm

โค้ด: เลือกทั้งหมด

try:
    driver.find_element_by_id("ID")
    return true
except NoSuchElementException:
    return false
ต้องเอา
try: except มาช่วย บ้างที code มันหาค่าอะไรไม่เจอ เราก้อดักมันว่าถ้าไม่เจอ อย่า error ขึ้นมานะ ให้ไปทำ บลา.... อะไรเเบบบนี้

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย natnicha001 » 16/09/2017 4:41 pm

ตอนนี้ตัดตรงนั้นแล้วค่ะ แต่ก็ยังไม่ได้อยู่ดี

โค้ด: เลือกทั้งหมด

time.sleep(5)
driver.is_element_present(By.NAME, "post")
error เหมือนเดิมค่ะ หรือต้อง ประกาศ อะไรไหมคะ :(

โค้ด: เลือกทั้งหมด

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 39, in <module>
    driver.is_element_present(By.NAME, "post")
AttributeError: 'WebDriver' object has no attribute 'is_element_present'
ขอบคุณล่วงหน้าคะ

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย thatsawan » 16/09/2017 3:53 pm

ต้องการจะเขียนอะไร

โค้ด: เลือกทั้งหมด

assertTrue 
เอามาใช้ทำอะไร ค่ะ

ถ้าไม่ได้ใช้ก้อไม่ต้องใส่เข้าไป

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย natnicha001 » 16/09/2017 3:47 pm

ขอโทษค่ะ แต่ไม่เข้าใจจริง ๆ ว่าถ้ามันใช้ไม่ได้แล้วเราควรทำไงคะ แล้วคำว่า assertTrue เป็นรูปแบบการเขียนที่ตายตัว หรือเป็นตัวแปลที่เปลี่ยนไปตาม code ของเราคะ ขอโทษค่ะ ไม่เข้าใจจริง ๆ

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย thatsawan » 16/09/2017 3:34 pm

error มันบอกว่า driver.assertTrue ใช้ไม่ได้

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย natnicha001 » 16/09/2017 10:20 am

จากการใช้ is_element_present ซึ่งหนูไม่แน่ใจว่าเขียนรูปแบบถูกหรือไม่ หรือแทนตัวแปรถูกไหม

โค้ด: เลือกทั้งหมด

time.sleep(5)
driver.assertTrue(driver.is_element_present(By.NAME, "post"))
error ที่เกิดขึ้น

โค้ด: เลือกทั้งหมด

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 39, in <module>
    driver.assertTrue(driver.is_element_present(By.NAME, "post"))
AttributeError: 'WebDriver' object has no attribute 'assertTrue'

Re: การเขียน selenium webdriver ด้วยภาษา python

โดย mindphp » 15/09/2017 11:47 am

natnicha001 เขียน:จากการใช้

โค้ด: เลือกทั้งหมด

driver.find_element_by_xpath("//input[@name=\"post\"]").click()
มี error ดังนี้

โค้ด: เลือกทั้งหมด

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 37, in <module>
    driver.find_element_by_xpath("//input[@name=\"post\"]").click()
  File "/home/com010/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 77, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/home/com010/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 493, in _execute
    return self._parent.execute(command, params)
  File "/home/com010/.local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
    self.error_handler.check_response(response)
  File "/home/com010/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not visible
  (Session info: chrome=61.0.3163.79)
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.10.0-33-generic x86_64)
ตาม Error
มันบอกว่า ว่า element not visible แปลว่ายังมองไม่เห็น element นั้น
ปัญหาอาจเป็นเพราะ หน้าที่เราต้องการให้ selenium หา element ยังโหลดหน้านั้นมายังไม่เสร็จ
ทางแก้ให้ลอง ทำดังนี้
ตอนคลิกลิงค์ก่อนมาคำสั่งนี้ ให้ใส่ตัวหน่วงเวลาเข้าไปก่อน

ศึกษาเพิ่มเติมการใช้งาน

driver.implicitly_wait(30)
หรือ
time.sleep(1)

และ is_element_present เพื่อช่วยลดข้อผิดพลาด ก่อนใช้คำสั่ง find_element_by_***

ข้างบน