Как выбрать указанный элемент c, чтобы он соответствовал определенному значению на веб-странице? (python селен ткинтер) - PullRequest
1 голос
/ 05 апреля 2020

Следующая программа автоматически вводит данные от GUI до webrowser. Значение "class" для этого dataset равно 65.

uiexample

Я пытаюсь сделать так, чтобы код выбрал значение 65 на веб-сайте. Категория «класс» состоит из выпадающего меню.

website example

Код элемента проверки:

<select _ngcontent-c37="" class="input productEntry ng-pristine ng-valid ng-touched" formcontrolname="class" tabindex="1021">
                        <option _ngcontent-c37="" value="0">N/A</option>
                        <!----><option _ngcontent-c37="" value="50" class="ng-star-inserted">50</option><option _ngcontent-c37="" value="55" class="ng-star-inserted">55</option><option _ngcontent-c37="" value="60" class="ng-star-inserted">60</option><option _ngcontent-c37="" value="65" class="ng-star-inserted">65</option><option _ngcontent-c37="" value="70" class="ng-star-inserted">70</option><option _ngcontent-c37="" value="77" class="ng-star-inserted">77.5</option><option _ngcontent-c37="" value="85" class="ng-star-inserted">85</option><option _ngcontent-c37="" value="92" class="ng-star-inserted">92.5</option><option _ngcontent-c37="" value="100" class="ng-star-inserted">100</option><option _ngcontent-c37="" value="110" class="ng-star-inserted">110</option><option _ngcontent-c37="" value="125" class="ng-star-inserted">125</option><option _ngcontent-c37="" value="150" class="ng-star-inserted">150</option><option _ngcontent-c37="" value="175" class="ng-star-inserted">175</option><option _ngcontent-c37="" value="200" class="ng-star-inserted">200</option><option _ngcontent-c37="" value="250" class="ng-star-inserted">250</option><option _ngcontent-c37="" value="300" class="ng-star-inserted">300</option><option _ngcontent-c37="" value="400" class="ng-star-inserted">400</option><option _ngcontent-c37="" value="500" class="ng-star-inserted">500</option>
                    </select>

Python код:

orders = {}
def order_data():

    orders[len(orders)] = {}
    orders[len(orders) - 1]['handling unit'] = e3.get()
    orders[len(orders) - 1]['pieces'] = e4.get()
    orders[len(orders) - 1]['description'] = e5.get()
    orders[len(orders) - 1]['length'] = e6.get()
    orders[len(orders) - 1]['width'] = e7.get()
    orders[len(orders) - 1]['height'] = e8.get()
    orders[len(orders) - 1]['weight'] = e9.get()
    orders[len(orders) - 1]['classification'] = e10.get()               

def GetQuote():
    #autofill origin zip, destination zip, and product fields
    driver.implicitly_wait(1)
    driver.find_element(By.XPATH, "//input[@name='originZip']").send_keys(origin_zip)
    driver.find_element(By.XPATH, "//input[@name='destinationZip']").send_keys(destination_zip)

    for i in range(len(orders)):
        #Product
        driver.find_element(By.XPATH, "/html[1]/body[1]/app-root[1]/div[1]/div[1]/app-record[1]/div[1]/div[2]/div[1]/app-record-quoting[1]/div[1]/app-record-product-list-panel[1]/form[1]/div[3]/div[1]/div[1]/div[1]/input[1]").send_keys(orders[i]['description'])

        #dropdown menu for Class
        select_element = Select(driver.find_element_by_xpath("/html[1]/body[1]/app-root[1]/div[1]/div[1]/app-record[1]/div[1]/div[2]/div[1]/app-record-quoting[1]/div[1]/app-record-product-list-panel[1]/form[1]/div[3]/div[1]/div[2]/div[1]/select[1]"))

        select_element.select_by_value(orders[i]['classification']) #<this is what I tried

Я получаю следующую ошибку:

raise NoSuchElementException("Cannot locate option with value: %s" % value)
selenium.common.exceptions.NoSuchElementException: Message: Cannot locate option with value: 65

Каков наилучший способ решить эту проблему?

Ответы [ 2 ]

1 голос
/ 05 апреля 2020

Я только что добавил метод implicitly_wait, и он работал!

def GetQuote():
    for i in range(len(orders)):
        #Product
        driver.find_element(By.XPATH, "/html[1]/body[1]/app-root[1]/div[1]/div[1]/app-record[1]/div[1]/div[2]/div[1]/app-record-quoting[1]/div[1]/app-record-product-list-panel[1]/form[1]/div[3]/div[1]/div[1]/div[1]/input[1]").send_keys(orders[i]['description'])

        #dropdown menu for Class
        select_element = Select(driver.find_element_by_xpath("/html[1]/body[1]/app-root[1]/div[1]/div[1]/app-record[1]/div[1]/div[2]/div[1]/app-record-quoting[1]/div[1]/app-record-product-list-panel[1]/form[1]/div[3]/div[1]/div[2]/div[1]/select[1]"))
        driver.implicitly_wait(10)
        select_element.select_by_value(orders[i]['classification'])
0 голосов
/ 05 апреля 2020

Попробуйте добавить больше времени или используйте wait element_to_be_clickable .

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "your Element"))
    )
finally:
    driver.quit()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...