Hướng dẫn python open webpage and click button - python mở trang web và nhấp vào nút

Tôi hiện có một tập lệnh đăng nhập cho tôi vào một trang web và tôi muốn có nó nhấp vào một nút trên trang web nếu nó hiện không được nhấp. Đây là thông tin cho nút:

Khi nút đã hoạt động:

Khi nút không hoạt động:

Tôi chỉ đang tìm cách nhấp vào nó khi class="button grey toast track-click"

Cách tốt nhất để làm việc này là gì? Tôi hiện đang sử dụng urllib2 và cơ giới hóa để đăng nhập và kiểm tra một vài biểu mẫu hiện tại. Cảm ơn!

Đã hỏi ngày 9 tháng 1 năm 2015 lúc 21:06Jan 9, 2015 at 21:06

Hướng dẫn python open webpage and click button - python mở trang web và nhấp vào nút

1

Khi tôi so sánh hai thẻ, tôi thấy rằng sự khác biệt là cho thẻ lớp. Vì vậy, nếu bạn có thể đọc nó thì bạn đã hoàn thànhclass tag. So if you can read it then you're done

Bạn có thể làm điều đó với Selenium nếu bạn thích

Bước 1: Tìm XPath - Nhận Xpath của nút: Để mở trang đó, hãy nhấp vào chrome, nhấp vào nó và chọn Kiểm tra phần tử - nó sẽ mở tệp HTML và nhấp chuột phải vào dòng được tô sáng và chọn Sao chép XPath - Sao chép Xpath trong notepad

Bây giờ bạn có XPath bạn có thể chọn nút thông qua tập lệnh Python và truy vấn các thuộc tính

Đây là một nguyên mẫu

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.youradress.org")#put here the adress of your page
elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
button = driver.find_element_by_id('buttonID') //Or find button by ID.
print(elem.get_attribute("class"))
driver.close()

Hy vọng điều đó sẽ giúp, nếu bạn có câu hỏi, vui lòng cho tôi biết

Tôi đã sử dụng các liên kết này để tài liệu

Python selenium: Tìm các thuộc tính đối tượng bằng cách sử dụng XPath

https://selenium-python.readthedocs.io/locating-elements.html

Hướng dẫn python open webpage and click button - python mở trang web và nhấp vào nút

Haseeb Mir

8611 Huy hiệu vàng13 Huy hiệu bạc21 Huy hiệu đồng1 gold badge13 silver badges21 bronze badges

Đã trả lời ngày 9 tháng 1 năm 2015 lúc 21:36Jan 9, 2015 at 21:36

GabrielgabrielGabriel

3,5141 Huy hiệu vàng25 Huy hiệu bạc48 Huy hiệu đồng1 gold badge25 silver badges48 bronze badges

4

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcfind_element_by_link_text() which scrapes the element using the text present. In case there is no such element with the given text attribute, NoSuchElementException is returned.

    Installation:

    Đảm bảo rằng bạn đã cài đặt selen bằng cách sử dụng

    pip3 install Selenium

    Và cũng tải xuống WebDriver cho trình duyệt web của bạn:

    Chrome : https://chromedriver.chromium.org/downloads
    Firefox : https://github.com/mozilla/geckodriver/releases
    Safari : https://webkit.org/blog/6900/webdriver-support-in-safari-10/

    Sau khi selen được cài đặt cùng với webDriver mong muốn, chúng tôi tạo một tập lệnh tệp và sử dụng trình chỉnh sửa mã của chúng tôi, hãy viết tập lệnh Python bên dưới, mở ra trang web GeekSforGeek sử dụng selenium webdriver và nhấp vào nút Đăng nhập bằng văn bản liên kết.

    Syntax:

    driver.find_element_by_link_text("sample text")

    Cách tiếp cận từng bước:

    • Nhập các mô -đun yêu cầu.
    • Tạo đối tượng WebDriver.
    • Chỉ định URL.
    • Sử dụng phương thức Maximize_window () để tối đa hóa cửa sổ trình duyệt. Và sau đó đợi 10 giây bằng phương pháp Sleep ().maximize_window() method to maximize the browser window. And then wait 10 seconds using sleep() method.
    • Sử dụng phương thức find_element_by_link_text () để nhấp vào nút theo văn bản.find_element_by_link_text() method to click button by text.

    Dưới đây là việc thực hiện.

    Python3

    from selenium import

    0

    import

    2

    3

    4

    5

    6

    7

    8

    9
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.youradress.org")#put here the adress of your page
    elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
    button = driver.find_element_by_id('buttonID') //Or find button by ID.
    print(elem.get_attribute("class"))
    driver.close()
    
    0

    7

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.youradress.org")#put here the adress of your page
    elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
    button = driver.find_element_by_id('buttonID') //Or find button by ID.
    print(elem.get_attribute("class"))
    driver.close()
    
    2

    4
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.youradress.org")#put here the adress of your page
    elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
    button = driver.find_element_by_id('buttonID') //Or find button by ID.
    print(elem.get_attribute("class"))
    driver.close()
    
    4
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.youradress.org")#put here the adress of your page
    elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
    button = driver.find_element_by_id('buttonID') //Or find button by ID.
    print(elem.get_attribute("class"))
    driver.close()
    
    5

    7

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Firefox()
    driver.get("http://www.youradress.org")#put here the adress of your page
    elem = driver.find_elements_by_xpath("//*[@type='submit']")#put here the content you have put in Notepad, ie the XPath
    button = driver.find_element_by_id('buttonID') //Or find button by ID.
    print(elem.get_attribute("class"))
    driver.close()
    
    7

    Output:

    https://media.geeksforgeeks.org/wp-content/uploads/20210222231422/output_FTOFsx0Z_Tx7e.mp4

    Đầu tiên, WebDriver mở cửa sổ bằng GeekSforGeek, tối đa hóa nó và chờ đợi trong 10 giây. Sau đó, nó nhấp vào nút Đăng nhập và mở bảng đăng ký.

    Làm cách nào để tự động nhấp vào nút trên trang web?

    Cách tự động hóa các nhấp chuột bằng JavaScript..
    Bấm vào đây.
    tài liệu.getEuityById ("clickme").nhấp chuột();.
    for (let i = 0; i <1000; i ++) {document.getEuityById ("clickme"). click ();}.

    Làm cách nào để mở một trang web với Python?

    Chỉ cần mở trình thông dịch Python và nhập webbrowser.open ('http://www.google.com') và xem liệu nó có làm những gì bạn muốn không.Vâng.open the python interpreter and type webbrowser. open('http://www.google.com') and see if it does what you want. yes.

    Làm thế nào để bạn nhấp vào một nút cụ thể trong Python?

    Chúng tôi có thể nhấp vào một nút với selenium webdriver trong Python bằng phương thức Click.Đầu tiên, chúng tôi phải xác định nút để được nhấp với sự trợ giúp của bất kỳ trình định vị nào như ID, Tên, Lớp, XPath, TagName hoặc CSS.Sau đó, chúng tôi phải áp dụng phương thức nhấp vào nó.Một nút trong mã HTML được biểu thị bằng tên nút.using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.