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

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

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

ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

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_***
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย natnicha001 »

จากการใช้ 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'
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

error มันบอกว่า driver.assertTrue ใช้ไม่ได้
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย natnicha001 »

ขอโทษค่ะ แต่ไม่เข้าใจจริง ๆ ว่าถ้ามันใช้ไม่ได้แล้วเราควรทำไงคะ แล้วคำว่า assertTrue เป็นรูปแบบการเขียนที่ตายตัว หรือเป็นตัวแปลที่เปลี่ยนไปตาม code ของเราคะ ขอโทษค่ะ ไม่เข้าใจจริง ๆ
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

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

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

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

ถ้าไม่ได้ใช้ก้อไม่ต้องใส่เข้าไป
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย natnicha001 »

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

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

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'
ขอบคุณล่วงหน้าคะ
ภาพประจำตัวสมาชิก
thatsawan
PHP VIP Members
PHP VIP Members
โพสต์: 28508
ลงทะเบียนเมื่อ: 31/03/2014 10:02 am
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย thatsawan »

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

try:
    driver.find_element_by_id("ID")
    return true
except NoSuchElementException:
    return false
ต้องเอา
try: except มาช่วย บ้างที code มันหาค่าอะไรไม่เจอ เราก้อดักมันว่าถ้าไม่เจอ อย่า error ขึ้นมานะ ให้ไปทำ บลา.... อะไรเเบบบนี้
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย natnicha001 »

หมายความอย่างนี้หรอคะ 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'
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ปุ่มชื่อ โพสต์ ใช้ 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()
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
Forexduck
PHP Sr. Member
PHP Sr. Member
โพสต์: 72
ลงทะเบียนเมื่อ: 26/05/2022 11:48 pm

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

โพสต์ที่ยังไม่ได้อ่าน โดย Forexduck »

โห แอดมินเก่งจังเลยครับ
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 88