как нажать кнопку, используя find_element_by_xpath с внутренним html в селене webdriver python3.7 - PullRequest
0 голосов
/ 11 октября 2018
first_click_content = driver.find_element_by_xpath("//div[@class='recent-report-wrapper']/div[2]/div/div[6]/div[2]")

print(first_click_content.get_attribute('innerHTML')

Приведенный выше код дает такой результат:

<button class="buttonWhite js-report-rerun">Re-run</button>
<button class="buttonWhite marginLeft js-report-edit">Edit</button>
<button class="buttonWhite marginLeft js-report-remove">Remove</button>
<button class="buttonWhite marginLeft js-report-save" style="display: none;">Save </button>
<button class="buttonWhite marginLeft js-report-view-errors" style="display: none;">View Errors</button>
<button class="buttonReportGreen marginLeft js-report-view" style="display: none;">View</button>
<button class="buttonReportGreen marginLeft js-report-download" style="display: inline-block;">Download</button>

Я хочу нажать первую кнопку, как я могу это сделать?

1 Ответ

0 голосов
/ 11 октября 2018

Поскольку все кнопки являются JavaScript включенным элементом, вам нужно заставить WebDriverwait для желаемого элемента быть активным , и вы можете использовать любой из следующихрешения:

  • CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.buttonWhite.js-report-rerun"))).click()
    
  • XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='buttonWhite js-report-rerun' and contains(.,'Re-run')]"))).click()
    
  • Примечание. Необходимо добавить следующие операции импорта:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...