使用Python Selenium访问下拉元素
因此,我试图找出如何从Python web driver for selenium中获取正确的cssSelector来访问html元素 我有一个页面,其中有两个下拉选项。我想选择显示“快速模式”的选项,然后使用Python web驱动程序在下拉列表中选择第二个选项 左侧类似的下拉列表也有类似的元素使用Python Selenium访问下拉元素,python,selenium,xpath,css-selectors,webdriverwait,Python,Selenium,Xpath,Css Selectors,Webdriverwait,因此,我试图找出如何从Python web driver for selenium中获取正确的cssSelector来访问html元素 我有一个页面,其中有两个下拉选项。我想选择显示“快速模式”的选项,然后使用Python web驱动程序在下拉列表中选择第二个选项 左侧类似的下拉列表也有类似的元素 <a class="btn-pill dropdown-toggle active" href="#" data-dialog-id="dialog-view28363">
<a class="btn-pill dropdown-toggle active" href="#" data-dialog-id="dialog-view28363"> <i class="message-indicator icon-info-circle" style=""></i> Job<span class="caret"></span> </a>
您的屏幕截图不会显示选择“快速模式”后出现的任何选项,因此很难为您提供选择该模式的帮助。但是,“快速模式”确实有一个独特的类,“下拉切换搜索模式”
我之所以避免使用“数据对话框id”属性,只是因为我怀疑它与每个产品构建不一致,但如果您确定始终存在一对一关联,您可以使用它,但只能在单击打开它的链接(快速模式按钮)后使用它.我在java selenium中完成了一个自动化项目,在该项目中,我对下拉列表执行操作 //页内对象
public static WebElement idProof(WebDriver driver)
{
WebElement element=null;
WebDriverWait wait=new WebDriverWait(driver, 50);
element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='panel-body']//div[3]//div[2]//div[1]//a[1]//span[1]")));
return element;
}
public static WebElement idProofVoterId(WebDriver driver, String idVal)
{
WebElement element=null;
WebDriverWait wait=new WebDriverWait(driver, 50);
element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(text(),'" + idVal +"')]")));
return element;
}
//在测试文件中
{ WebElement idProof = FrmrPage.idProof(driver);
idProof.click();
Genlib.sleep(2000);
WebElement voterId = FrmrPage.idProofVoterId(driver, datArr[8]);
voterId.click();
test.pass("ID Proof: " + datArr[8]);
Genlib.sleep(1000);
}
要在文本为“作业”的元素上单击(),您必须诱导WebDriverWait使该元素可单击,并且可以使用以下任一选项:
- 使用部分链接文本:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Job"))).click()
- 使用XPATH A:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-pill dropdown-toggle active' and contains(., 'Job')]"))).click()
- 使用XPATH B:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-pill dropdown-toggle active' and normalize-space()='Job']"))).click()
- 注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn-pill dropdown-toggle active' and normalize-space()='Job']"))).click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC