如何使用Selenium chromewebdriver / python选择/单击下拉内容

时间:2019-04-06 03:41:29

标签: python selenium xpath css-selectors chrome-web-driver

我的网站表单已更新,我的脚本不再可用。我无法解决它,因为我无法找到如何使用Selenium chrome Web驱动程序和python在下拉菜单内容中进行选择。

formattedstate是格式化数据的值(从文件中读取)

driver.find_element_by_css_selector(f"option[title='{formattedState}']").click()

driver.find_element_by_xpath("//*[text()='{formattedState}']").click()

driver.find_element_by_css_selector(f"option[value='{formattedState}']").click()

这是来自网络表单的下拉内容的数据。我在下拉菜单中选择了第一个州=阿拉巴马州

<input aria-valuetext="" role="combobox" tabindex="0" 
placeholder="State/Province/Region" readonly="" height="100%" size="1" autocomplete="off" value="" data-spm-anchor-id="a2g0o.placeorder.0.i14.5f0b321eC7zfLx">



<li class="next-menu-item" title="Alabama" data-spm-anchor-id="a2g0o.placeorder.0.i17.5f0b321eC7zfLx">Alabama</li>

它应该在下拉菜单中选择正确的状态

2 个答案:

答案 0 :(得分:1)

首先尝试单击组合框,然后依次单击wait到状态选项(li)元素可见并单击。

在下面的代码中,我使用css选择器通过li获取title。如果要按文本查找元素,请使用:
wait.until(ec.visibility_of_element_located((By.XPATH, f"//li[.='{state}' and @class = 'next-menu-item']"))).click()

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

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

state = "Alabama"
driver.find_element_by_css_selector('input[placeholder="State/Province/Region"]').click()
wait.until(ec.visibility_of_element_located((
    By.CSS_SELECTOR, f"li.next-menu-item[title='{state}']"))).click()

答案 1 :(得分:0)

JS SetAttribute对于Combobox效果惊人。试试

import numpy, scipy, matplotlib
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

xData = numpy.array([1.1, 2.2, 3.3, 4.4, 5.0, 6.6, 7.7])
yData = numpy.array([1.1, 20.2, 30.3, 60.4, 50.0, 60.6, 70.7])


def func(x, a, b, c): # simple quadratic example
    return (a * numpy.square(x)) + b * x + c


# these are the same as the scipy defaults
initialParameters = numpy.array([1.0, 1.0, 1.0])

# curve fit the test data
fittedParameters, pcov = curve_fit(func, xData, yData, initialParameters)

modelPredictions = func(xData, *fittedParameters) 

absError = modelPredictions - yData

SE = numpy.square(absError) # squared errors
MSE = numpy.mean(SE) # mean squared errors
RMSE = numpy.sqrt(MSE) # Root Mean Squared Error, RMSE
Rsquared = 1.0 - (numpy.var(absError) / numpy.var(yData))

print('Parameters:', fittedParameters)
print('RMSE:', RMSE)
print('R-squared:', Rsquared)

print()


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = func(xModel, *fittedParameters)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    plt.show()
    plt.close('all') # clean up after using pyplot

graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)