python-3.x 尝试使用ID而不是XPATH

xdyibdwo  于 2023-05-19  发布在  Python
关注(0)|答案(2)|浏览(108)

到目前为止,这段代码运行良好。但是当我使用这个函数迭代一个电子表格后,它不会点击“更多详细信息”。它只会使用一个包裹编号。我假设这是因为我使用的是XPATH而不是ID,不确定。有谁能帮我找到正确的元素吗?

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import time

def gis_website():
    """Navigate to the GIS website, search for a parcel, and click the search button."""
    try:
        options = webdriver.ChromeOptions()
        #options.add_argument('--headless')
        #options.headless = True
        driver = webdriver.Chrome(options=options)
        driver.get("https://isv.kcsgis.com/al.etowah_revenue/")
        time.sleep(10)  # Increase wait time to 10 seconds

        button = driver.find_element(By.XPATH, '/html/body/div[6]/div[2]/div[2]/button')
        button.click()

        # If the element appears, the accept button has been clicked successfully
        print("Accept button clicked successfully!")

        # Wait for the "Go to Map" button to appear and then click it
        wait = WebDriverWait(driver, 10)
        go_to_map = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[2]/aside/button')))
        driver.execute_script("arguments[0].click();", go_to_map)
        print("Go to map button clicked successfully!")

        # Wait for the overlay to disappear or become invisible
        wait = WebDriverWait(driver, 10)
        overlay = wait.until(EC.invisibility_of_element_located((By.CLASS_NAME, "dijitDialogUnderlay")))
        
        # click the parcel number box
        input_box = driver.find_element(By.XPATH, "/html[@class='dj_webkit dj_chrome dj_contentbox blueTheme has-webkit has-no-quirks']/body[@class='dbootstrap']/div[@id='borderContainerOuter']/div[@id='sidebarLeft']/div[@id='search_parent']/div[@class='dijitTitlePaneContentOuter']/div[@class='dijitReset']/div[@id='search_parent_pane']/div[@id='search_widget']/div[@id='dijit_layout_TabContainer_0']/div[@class='dijitTabPaneWrapper dijitTabContainerTop-container dijitAlignCenter']/div[@class='dijitTabContainerTopChildWrapper dijitVisible']/div[@id='dijit_layout_ContentPane_0']/div[@id='divAttributeQueryText']/div[@id='widget_dijit_form_TextBox_2']/div[@class='dijitReset dijitInputField dijitInputContainer']/input")
        ActionChains(driver).move_to_element(input_box).click().perform()

        # Send keys to the input box element
        input_box.send_keys("1009314000067000")
        time.sleep(5)
        
        # locate the search button and click it
        #search_button = driver.find_element(By.XPATH, "//span[@id='dijit_form_Button_1_label']")
        search_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[@id='dijit_form_Button_1_label']")))
        search_button.click()
        time.sleep(5)

        print("Parcel number typed in and search button clicked successfully!")
    
        more_details = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div/div[3]/div/div/div[2]/div[2]/div/div[1]/table/tr/td[8]')
        more_details.click()
        
        
        print("More detials clicked succesfully")
        
        # Wait for the new tab to open
        wait.until(EC.number_of_windows_to_be(2))

        # Get the handles of all open windows/tabs
        window_handles = driver.window_handles

        # Switch to the newly opened tab (the last one in the list)
        driver.switch_to.window(window_handles[-1])

        # Get the URL of the current window
        new_window_url = driver.current_url

        # Print the URL
        print(new_window_url)
       
    except Exception as e:
        print(e)

    finally:
        driver.quit()

gis_website()

这是代码中唯一需要正确元素的部分:

more_details = driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div/div/div[3]/div/div/div[2]/div[2]/div/div[1]/table/tr/td[8]')
ql3eal8s

ql3eal8s1#

试试这个:

more_details = driver.find_element(By.XPATH, '//span[text()="More Details"]')
more_details.click()

或者尝试使用ExplicitWaits,如下所示:

wait = WebDriverWait(driver, 20)
more_details = wait.until(EC.element_to_be_clickable((By.XPATH, '//span[text()="More Details"]')))
more_details.click()
pxyaymoc

pxyaymoc2#

对于仅依赖ID,我建议用途:

//*/*[@Id='Id_of_btn']

导致:

driver.find_element(By.XPATH, '//*/*[@Id='Id_of_btn']').click()

使用'*'语法,您尝试查找哪个元素并不重要,只需关注ID。
有关详细信息,请参阅:
W3-Schools.xpath

相关问题