如何在Python上使用Selenium / Pyautogui从下拉列表中选择“全部”

时间:2017-10-08 06:14:34

标签: python selenium

如何在Python上使用Selenium / Pyautogui从下拉列表中选择“全部”

代码:

driver = webdriver.Chrome()
driver.get("http://www.google.com")
scrolldown = driver.execute_script("window.scrollTo(0, 
document.body.scrollHeight);")
displayall = driver.find_element_by_css_selector('.k-icon.k-i-arrow-
s').click()
x,y = pyautogui.position()
z = y+25
movemouse = pyautogui.moveTo(x,z, duration=0.25)
movemousepos = pyautogui.position()
pyautogui.click()

HTML:

<span title="" class="k-widget k-dropdown k-header" 
unselectable="on" role="listbox" aria-haspopup="true" aria-
expanded="false" tabindex="0" aria-owns="" aria-disabled="false" 
aria-readonly="false" aria-busy="false" style="" aria-
activedescendant="36e1dea4-df6d-4f58-afd5-77d93c0f7251">
<span unselectable="on" class="k-dropdown-wrap k-state-default">
<span unselectable="on" class="k-input">All</span>
<span unselectable="on" class="k-select">
<span unselectable="on" class="k-icon k-i-arrow-s">select</span>
</span></span>
<select data-role="dropdownlist" style="display: none;">
<option value="all" selected="selected">All</option>
<option value="10">10</option><option value="20">20</option>
<option value="50">50</option></select></span>

没有错误,但它无法正常工作。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我完全不知道selenium python,所以我给了你JAVA的代码。将其转换为Python。正如我所看到的HTML,从下拉列表中选择值非常困难,因此您可以使用JavascriptExecutor

System.setProperty("webdriver.chrome.driver","E:/software and tools/chromedriver_win32/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.thaibma.or.th/EN/News/Search.aspx");
WebElement ItemsPerPage= driver.findElement(By.xpath("//span[@class='k-icon k-i-arrow-s']"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click()", ItemsPerPage);
WebElement AllItem= driver.findElement(By.xpath(" //li[contains(@role,'option')][contains(text(),'All')]"));
js.executeScript("arguments[0].click()", AllItem);