Reputation: 470
I'm using Selenium with Python 3.5 to scrape various websites. However, I need to select from a drop-down list but keep getting an error. I'm quite embarrassed I can't get this to work, but XPath is brand new to me so your help is appreciated. The truncated version of my code is:
driver.implicitly_wait(4)
driver.find_element_by_xpath("//select[@id='ddaShowForm']/
option[@value='Deposits']/text()").click()
I have the wait there to make sure the page is fully loading because that was something that got me before. The error I'm getting is that it can't find the element. Thanks for your help!
Here is a snippet of the relevant area of the page. I'm sorry for the poor formatting.
<form id="ddaShowForm" action="https://online.wellsfargo.com/das/cgi-bin/session.cgi?sessargs=xUog_f8DdTZHif-5iVUuuwe9XQVqncvm" method="post">
<label for="transactionType">Show: </label>
<select id="transactionType" name="showTabDDACommand.transactionTypeFilterValue">
<option value="All Transactions" selected="selected">All Transactions</option>
<option value="All Transactions One Column">All Transactions in One Column</option>
<option value="All Transactions with Balance">All Transactions with Balances</option>
<option value="Bill Pay">Bill Pay Transactions</option>
<option value="Check Card">Debit Card / ATM Card Transactions</option>
<option value="Checks">Checks</option>
<option value="Deposits">Deposits</option>
<option value="Withdrawls">Withdrawals</option>
<option value="transactionTypeFilter.p2p.label">WF SurePay</option>
<option value="transactionTypeFilter.wire.label">Wires</option>
</select>
<label for="timeFilter"> for </label>
<select id="timeFilter" name="showTabDDACommand.timeFilterValue" size="1">
<optgroup label="" >
<option value="3" selected="selected" >Last 90 Days</option>
<option value="4" >Last 6 Months</option>
<option value="5" >Last 12 Months</option>
<option value="6" >Last 18 Months</option>
<option value="7" >Since Last Logon</option>
<option value="8" >Since Last Statement</option>
<optgroup label="--------------------" class="optgroupstyle"></optgroup>
<option value="11" >Date Range</option>
</optgroup>
</select>
Upvotes: 0
Views: 267
Reputation: 470
Thanks to the help received by pArAs, I would like to answer the original XPath question for anyone who stumbles upon this in the future. His method pointed out to me that the "ddaShowForm" is actually a form and not a 'select' element. The correct syntax for my original XPath is
driver.find_element_by_xpath("//select[@id='transactionType']/
option[@value='Deposits']").click()
If you look at the html, the 'select' element actually has id "transactionType". I would like to point out that I also removed the "/text()" at the end. I hope this helps.
Upvotes: 0
Reputation: 3235
For Selecting an element from dropdown you can use Select
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_id("transactionType"))
Then you can select using any of the methods provided by Select
select.select_by_visible_text("All Transactions")
select.select_by_value("All Transactions")
select.select_by_index(1)
Upvotes: 1