Q - ต้องการให้ Selenium นั้นคลิกปุ่มกด Like ที่บทความ

ตอบกระทู้

รูปแสดงอารมณ์
: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] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: Q - ต้องการให้ Selenium นั้นคลิกปุ่มกด Like ที่บทความ

Re: Q - ต้องการให้ Selenium นั้นคลิกปุ่มกด Like ที่บทความ

โดย rangsan » 16/05/2018 7:58 pm

thatsawan เขียน:เราสามารถ xpath ลงมาเรื่อยได้
เช่น

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

//notebook/page/field[@name='order_line']/tree/field[@name='discount']"

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

if(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Like[\\s\\S]*$")== true) {
driver.findElement(By.xpath("//form[@id='u_0_1']/div/div/div/button")).click();
}
ลองดูนะ

ได้ทดลองทำตามที่บอกแล้วครับแต่ก็ยังไม่ได้ครับ

โค้ดที่ได้ลองทำ

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

        if driver.find_element_by_css_selector("body"):
            driver.find_element_by_xpath("//form[@id='u_0_1']/div/div/div/button").click()
        else:
            print("No click")
ส่วนของ Error

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

Traceback (most recent call last):
  File "/home/com003/PycharmProjects/m058/selenium_autorat.py", line 78, in <module>
    driver.find_element_by_xpath("//form[@id='u_0_1']/div/div/div/button").click()
  File "/home/com003/PycharmProjects/m058/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 385, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/home/com003/PycharmProjects/m058/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element
    'value': value})['value']
  File "/home/com003/PycharmProjects/m058/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
    self.error_handler.check_response(response)
  File "/home/com003/PycharmProjects/m058/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //form[@id='u_0_1']/div/div/div/button

Re: Q - ต้องการให้ Selenium นั้นคลิกปุ่มกด Like ที่บทความ

โดย thatsawan » 12/05/2018 4:34 pm

เราสามารถ xpath ลงมาเรื่อยได้
เช่น

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

//notebook/page/field[@name='order_line']/tree/field[@name='discount']"

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

if(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Like[\\s\\S]*$")== true) {
driver.findElement(By.xpath("//form[@id='u_0_1']/div/div/div/button")).click();
}
ลองดูนะ

Q - ต้องการให้ Selenium นั้นคลิกปุ่มกด Like ที่บทความ

โดย rangsan » 12/05/2018 4:09 pm

ตอนนี้ผมกำลังทำ selenium เพื่อให้เข้าไปกดโหวตให้คะแนนบทความและกดไลค์ให้กับบทความนั้น ๆ ซึ่งตอนนี้สามารถกดโหวตได้แต่ไม่สามารถกดไลค์ได้

ภาพตัวอย่างปุ่มไลค์และปุ่มโหวต
Selection_083.png
จะให้ว่ามี Source Code ข้างล่างซึ่งเป็นของปุ่ม ไลค์

ผมได้ลองใช้คำสั่งที่ค้นหามาจาก Google แต่ก็ยังไ่ม่สามารถกดไลค์ได้ครับ
ตัวอย่างคำสั่งที่ใช้

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

driver.find_element_by_css_selector('.inlineBlock._2tga._49ve').click()

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

driver.find_elements_by_xpath("//button[@class='inlineBlock _2tga _49ve']")

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

driver.find_element_by_class_name('_3jn-.inlineBlock').click()

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

driver.find_element_by_xpath("//*[@id='u_0_2']").click()
นี่คือในส่วนที่ผมได้ลองทดสอบใช้ไปครับแต่ก็ไม่สามารถคลิกได้ไม่แน่ใจว่าผมอาจจะใส่พวก path หรือ เขียนโค้ดอะไรผิดไปหรือเปล่าครับ

ข้างบน