程序单击您需要的按钮。(Python)

2024-09-29 17:21:55 发布

您现在位置:Python中文网/ 问答频道 /正文

他用Python和Selenium编写了一个程序。在网站上,我有一个按钮:

<button class="_rfh _4jy0 _4jy4 _517h _51sy _42ft" type="submit" value="1"><i alt="" class="_3-8_ img sp_DV9drRVqFBy sx_08f171"></i>Udostępnij</button>

我在我的程序中写下,他会按下这个按钮:

driver.find_element_by_xpath('//*[@class="_4jy0 _4jy4 _517h _51sy _42ft"]').click()

但程序不会单击此按钮,而是单击此页面上的另一个按钮,如下所示:

<button class="likeButton _4jy0 _4jy4 _517h _51sy _42ft" data-testid="page_profile_like_button_test_id" type="submit" value="1"><i alt="" class="_3-8_ img sp_DV9drRVqFBy sx_e53c72"></i>Lubię to!</button>

有人知道为什么会这样吗


Tags: 程序imgvalue网站typeseleniumbuttonalt
2条回答

它不会粘贴所有代码,因为它太长了。发生错误的地方是:

 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//*[contains(text(), 'Udostępnij')]"))).click()
    if jakile == "nieskonczonosc":
        time.sleep(0.1)
    if jakile == "okreslonailosc":
        global i
        i += 1
    driver.find_element_by_xpath("//button[@class='mat-focus-indicator mat-icon-button mat-button-base mat-primary']").click()
    time.sleep(2)
    driver.switch_to.window(driver.window_handles[-1])
    time.sleep(2)
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//*[contains(text(), 'Udostępnij')]"))).click()
    time.sleep(1)
    driver.switch_to.window(driver.window_handles[0])
    time.sleep(2)
    if jakile == "nieskonczonosc":
        time.sleep(0.1)
    if jakile == "okreslonailosc":
        if i > ileraz:
            driver.quit()
            skonczono()
    try:
        testt = driver.find_element_by_xpath('/html/body/lp-root/lp-ui-wrapper/lp-main-ui/div[2]/lp-page-content/div/main/ng-component/ng-component/lp-available-actions-page/lp-card/lp-available-actions-list/lp-card/lp-overlay-wrapper/lp-card/div/lp-overlay-wrapper/lp-card/lp-card/div/mat-card/div[2]/span')
    except Exception:
        odpowiadanie_obserwowaniestronfb_2()
    driver.quit()
    zleskonczono()

请检查类是否是动态的,同时检查下面的xpath是否适合您

WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"//button[@type='submit' and @value='1']"))).click()

WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH,"//*[contains(text(), 'Udostępnij')]"))).click()

注意:请添加以下导入内容

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

相关问题 更多 >

    热门问题