Selenium 如何查找属于 'flex flex-col md:grid 类的一部分的按钮

问题描述 投票:0回答:1

在Python中使用Selenium,我想单击一个带有文本“View Active”的按钮,但是Selenium在搜索“View Active”时找不到它。 查看带有“inspect”的代码(见下文),我看到包含按钮的“flex-col”类的以下内容。 我可以看到按钮文本,但我不知道如何从 Selenium 访问它

<div class="flex flex-col md:grid md:grid-cols-2 mt-7 flex-wrap gap-x-7 gap-y-7"><div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Moved from Registered Address</h4><p class="mt-1 mb-6">Review for consideration of challenge</p><div class="flex gap-3 flex-col md:flex-row"><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/assignedVoters">View All</a><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/assignedVoters?activeOnly=true">View Active</a></div></div><div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Non-Standard Addresses</h4><p class="mt-1 mb-6">Sorted by largest # of registrations</p><div class="flex gap-3 flex-col md:flex-row"><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/addressWithIssues">View All</a><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/addressWithIssues?activeOnly=true">View Active</a></div></div><div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Search by Name</h4><p class="mt-1 mb-6">Search for records by voter name</p><div class="flex gap-3 flex-col md:flex-row"><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/search/name">Search</a></div></div><div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Search by Address</h4><p class="mt-1 mb-6">Search for records by address</p><div class="flex gap-3 flex-col md:flex-row"><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/search/address">Search</a></div></div><div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">My Challenges</h4><p class="mt-1 mb-6">Review records selected for challenge</p><div class="flex gap-3 flex-col md:flex-row"><a class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center" href="/reviewChallenges">View Now</a></div></div></div>
python selenium-webdriver
1个回答
0
投票

我查看了你的代码,你所有的按钮都只是锚点,而不是真正的按钮,所以我会使用 XPath 并包含来自 selenium webdriver 和 python:

<div class="flex flex-col md:grid md:grid-cols-2 mt-7 flex-wrap gap-x-7 gap-y-7">
    <div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Moved from Registered Address</h4>
        <p class="mt-1 mb-6">Review for consideration of challenge</p>
        <div class="flex gap-3 flex-col md:flex-row"><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/assignedVoters">View All</a><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/assignedVoters?activeOnly=true">View Active</a></div>
    </div>
    <div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Non-Standard Addresses</h4>
        <p class="mt-1 mb-6">Sorted by largest # of registrations</p>
        <div class="flex gap-3 flex-col md:flex-row"><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/addressWithIssues">View All</a><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/addressWithIssues?activeOnly=true">View Active</a></div>
    </div>
    <div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Search by Name</h4>
        <p class="mt-1 mb-6">Search for records by voter name</p>
        <div class="flex gap-3 flex-col md:flex-row"><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/search/name">Search</a></div>
    </div>
    <div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">Search by Address</h4>
        <p class="mt-1 mb-6">Search for records by address</p>
        <div class="flex gap-3 flex-col md:flex-row"><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/search/address">Search</a></div>
    </div>
    <div class="bg-white shadow-chart px-8 py-6  w-full"><h4 class="font-bold">My Challenges</h4>
        <p class="mt-1 mb-6">Review records selected for challenge</p>
        <div class="flex gap-3 flex-col md:flex-row"><a
                class=" px-2 py-3 rounded-lg text-base hover:shadow-btn disabled:opacity-30 outline-none focus:ring-red focus:ring-2 w-44 bg-red text-white block text-center"
                href="/reviewChallenges">View Now</a></div>
    </div>
</div>

您可以使用 contains 过程来做到这一点。

options = webdriver.ChromeOptions()
options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'.  # replace with your path
options.add_argument('window-size=800x841')
# options.add_argument('headless')  # use this if you are running without a monitor
driver = webdriver.Chrome(chrome_options=options)
driver.get(url)
buttons = driver.find_elements_by_xpath('//a[contains(@class, "hover:shadow-btn")]').  # this is using a relative path to find all anchors with the class 'hover:shadow-btn'. you should be able to adapt this from here.  
for btn in buttons: 
     btn.click()
© www.soinside.com 2019 - 2024. All rights reserved.