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

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

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

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

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

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

การใช้งาน XPath ลองดูที่นี่
https://www.mindphp.com/forums/viewtopic ... 77&t=23622
ติดตาม 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 »

จากการใช้ submit() ไม่ error แต่ไม่มีอะไรเกิดขึ้น

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

driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').submit()
ส่วนการใช้ click()

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

driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').click()
มี error ดังนี้

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

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 30, in <module>
    driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').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.WebDriverException: Message: unknown error: Element is not clickable at point (614, 720)
  (Session info: chrome=61.0.3163.79)
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.10.0-33-generic x86_64)
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

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

จาก element

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

<fieldset class="submit-buttons">
			
<input type="hidden" name="lastclick" value="1505388154">

<input type="submit" accesskey="k" tabindex="7" name="save" value="บันทึกข้อความร่าง" class="button2">&nbsp; 	
		
<input type="submit" tabindex="5" name="preview" value="แสดงตัวอย่างหัวข้อ" class="button1" onclick="document.getElementById('postform').action += '#preview';">&nbsp;

<input type="submit" accesskey="s" tabindex="6" name="post" value="โพสต์" class="button1 default-submit-action">&nbsp;

</fieldset>
ต้องการ input[4]
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

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

รูปภาพ


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

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

# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Chrome('/usr/local/lib/python2.7/site-packages/chromedriver')
#เปิด ling mindphp
driver.get('https://www.mindphp.com/forums/index.php?sid=7e30176647e1b208585241f76022ae41')

#เปิด ling เข้าสู่ระบบ
driver.get('https://www.mindphp.com/forums/ucp.php?mode=login&sid=3b0d5a880c9645c9559b8bd015ae411f')

#พิมพ์ natnicha
user = driver.find_element_by_css_selector('#username')
user.send_keys('xxxxxxxx')

#พิมพ์ รหัส
password = driver.find_element_by_css_selector('#password')
password.send_keys('xxxxxxxx')

#คลิก login
login = driver.find_element_by_name('login')
login.click()

#เข้าเว็บ
driver.get('https://www.mindphp.com/forums/viewforum.php?f=6')

# คลิกเข้าปุ่มตอบกลับกระทู้
driver.find_element_by_xpath('//*[@id="page-body"]/div[2]/div[1]/a').click()

# พิมพ์ข้อความที่หัวกระทู้
title = driver.find_element_by_xpath('//*[@id="subject"]')
title.send_keys('title')

# พิมพ์ข้อความ test
Content = driver.find_element_by_xpath('//*[@id="message"]')
Content.send_keys('test1')

# คลิกปุ่มโพส
driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').click()
จากการทำงานทุกอย่าง ทำได้ตามปกติ แต่ error บรรทัด สุดท้าย คือการโพสข้อความ
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

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

error บรรทัด สุดท้าย
Error ว่าอะไร

หา ตำแหน่งไม่เจอ หรือ ใช้ .submit() ไม่ได้
ติดตาม 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 »

จากการใช้ .submit() ไม่เออเร่อ แต่ไม่สามารถโพสข้อความได้

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

driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').submit()
จากการใช้.click() error ตามข้างล่าง

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

driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').click()
error ที่เกิดขึ้น

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

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 30, in <module>
    driver.find_element_by_xpath('//*[@id="postform"]/div[2]/div/fieldset/input[4]').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.WebDriverException: Message: unknown error: Element is not clickable at point (614, 720)
  (Session info: chrome=61.0.3163.79)
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.10.0-33-generic x86_64)
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41251
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

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

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

ลองดูตามโค้ดนี้

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

        driver.find_element_by_xpath("//input[@name=\"post\"]").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
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

โพสต์ที่ยังไม่ได้อ่าน โดย 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)
ภาพประจำตัวสมาชิก
nuattawoot
PHP VIP Members
PHP VIP Members
โพสต์: 2561
ลงทะเบียนเมื่อ: 05/06/2017 9:34 am

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

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

ผมใช้โค้ดนี้ก้คลิกได้ ปกตินะ ลองใหม่ครับ

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

browser.find_element_by_name("post").click()
อันนีคลิกจาก link_text ไม่แนะนำ

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

browser.find_element_by_link_text(u"โพสต์").click()

ถ้าไม่ได้ให้ลองไปใช้ webdriver.Firefox ครับ
First Bug Love you
รูปภาพ
natnicha001
PHP Hero Member
PHP Hero Member
โพสต์: 120
ลงทะเบียนเมื่อ: 28/08/2017 10:14 am

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

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

จากการใช้

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

driver.find_element_by_name("post").click()
error

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

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 37, in <module>
    driver.find_element_by_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)

จากการใช้

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

driver.find_element_by_link_text(u"โพสต์").click()
error

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

Traceback (most recent call last):
  File "/home/com010/m050/test2.py", line 37, in <module>
    driver.find_element_by_link_text(u"โพสต์").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.WebDriverException: Message: unknown error: Element is not clickable at point (973, 936)
  (Session info: chrome=61.0.3163.79)
  (Driver info: chromedriver=2.30.477691 (6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.10.0-33-generic x86_64)

ไม่สามารถเปิดด้วย firefox

error

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

Traceback (most recent call last):
  File "/home/com010/m050/test3.py", line 2, in <module>
    driver = webdriver.Firefox()
  File "/home/com010/.local/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 142, in __init__
    self.service.start()
  File "/home/com010/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

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

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