Select hidden option value from source using python selenium chromedriver
<p>I am reading a Docx file [here is the <a href="https://www.dropbox.com/s/3za9qhzupc01azt/Taxatierapport%20SEK%20-%20Auto%20Centrum%20Bollenstreek%20-%20Peugeot%20308%20-%205603.docx?dl=0" rel="nofollow">link] </a>, parsing some text from that &amp; then Using python selenium bindings &amp; chrome-driver I am trying to click a Hidden option value from source (driver.page_source) . I know it isn't available to select. Here is my code so far :</p>

<pre><code>import time, re
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from docx import opendocx, getdocumenttext
from requests import Session

def read_return(word_file):
    document = opendocx(word_file)
    paratextlist = getdocumenttext(document)
    newparatextlist = []
    for paratext in paratextlist:
        newparatextlist.append((paratext.encode("utf-8")).strip('\n').strip('\t').strip('\r'))
    newparatextlist = str(newparatextlist).replace("]","").replace("[","")
    with open('sample.txt','wb')as writer:
        writer.write(newparatextlist)
    return newparatextlist

word_file = read_return('Taxatierapport SEK - Auto Centrum Bollenstreek - Peugeot 308 - 5603.docx')

x = lambda x:re.findall(x,word_file,re.DOTALL)[0].strip().replace("'","&amp;#39;").replace('"','&amp;#39;')
Voertuig = x("::OBJECT::', '(.+?)'")

Merk = x("::MERK::', '(.+?)'")
Model = x("::TYPE::', '(.+?)'")
TOELATING = x("::BOUWJAAR 1STE TOELATING::', '(.+?)'")
d1 = TOELATING.split("-")[0]
d2 = TOELATING.split("-")[1]
d3 = TOELATING.split("-")[2]
TRANSMISSIE = x("::TRANSMISSIE::', '(.+?)'")
BRANDSTOF = x("::BRANDSTOF::', '(.+?)'")

print "%r\n%r\n%r\n%r\n%r\n%r\n%r\n%r\n" %(Voertuig, Merk, Model, d1, d2, d3, TRANSMISSIE, BRANDSTOF)

if Voertuig == "Personenauto":
    value = 1
elif Voertuig == "Personenbussen":
    value = 7
elif Voertuig == "Bedrijfsauto&amp;#39;s tot 3.5 ton":
    value = 3
elif Voertuig == "Bedrijfsauto&amp;#39;s 4x4":
    value = 2
elif Voertuig == "Motoren":
    value= 5

xr = 0; yr = 0; zr = 1972
while xr &lt; 32:
    if int(d1) == xr:
        dvalue1 = xr
    else:
        pass
    xr+=1

while yr &lt; 13:
    if int(d2) == yr:
        dvalue2 = yr
    else:
        pass
    yr+=1

while zr &lt; 2018:
    if int(d3) == zr:
        dvalue3 = zr
    else:
        pass
    zr+=1

driver = webdriver.Chrome('chromedriver.exe')
driver.get('https://autotelexpro.nl/LoginPage.aspx')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_txtVestigingsnummer"]').send_keys('3783')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_txtGebruikersnaam"]').send_keys('Frank')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_Password"]').send_keys('msnauto2016')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_btnLogin"]').click()
time.sleep(10)
#try:
driver.find_element(By.XPATH, value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlVoertuigType"]/option[@value="'+str(value)+'"]').click()
driver.find_element(By.XPATH, value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwdag"]/option[@value="'+str(dvalue1)+'"]').click()
driver.find_element(By.XPATH, value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwmaand"]/option[@value="'+str(dvalue2)+'"]').click()
driver.find_element(By.XPATH, value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlBouwjaar"]/option[@value="'+str(dvalue3)+'"]').click()
driver.find_element(By.XPATH, value ='//select[@name="ctl00$cp$ucSearch_Manual$ddlMerk"]/option[@value="130"]').click()
#except:
driver.quit()

time.sleep(5)
driver.quit()
</code></pre>

<p>so Using requests module i make a POST request to the link &amp; manage to get a response that does have the needed options data , See here : </p>

<pre><code>&lt;select name="ctl00$cp$ucSearch_Manual$ddlMerk" onchange="updateInputForServerNoPB();InvalidateVehicleSearchResult();setTimeout(&amp;#39;__doPostBack(\&amp;#39;ctl00$cp$ucSearch_Manual$ddlMerk\&amp;#39;,\&amp;#39;\&amp;#39;)&amp;#39;, 0)" id="ctl00_cp_ucSearch_Manual_ddlMerk" class="NormalDropdownlist" style="width:174px;"&gt;
        &lt;option selected="selected" value="-1"&gt;- Kies merk -&lt;/option&gt;
        &lt;option value="95"&gt;Alfa Romeo&lt;/option&gt;
        &lt;option value="154"&gt;Aston Martin&lt;/option&gt;
        &lt;option value="96"&gt;Audi&lt;/option&gt;
        &lt;option value="97"&gt;Bentley&lt;/option&gt;
        &lt;option value="98"&gt;BMW&lt;/option&gt;
        &lt;option value="352"&gt;Bugatti&lt;/option&gt;
        &lt;option value="100"&gt;Cadillac&lt;/option&gt;
        &lt;option value="342"&gt;Chevrolet&lt;/option&gt;
        &lt;option value="101"&gt;Chevrolet USA&lt;/option&gt;
        &lt;option value="102"&gt;Chrysler&lt;/option&gt;
        &lt;option value="103"&gt;Citroen&lt;/option&gt;
        &lt;option value="337"&gt;Corvette&lt;/option&gt;
        &lt;option value="104"&gt;Dacia&lt;/option&gt;
        &lt;option value="105"&gt;Daihatsu&lt;/option&gt;
        &lt;option value="166"&gt;Daimler&lt;/option&gt;
        &lt;option value="162"&gt;Dodge&lt;/option&gt;
        &lt;option value="106"&gt;Donkervoort&lt;/option&gt;
        &lt;option value="107"&gt;Ferrari&lt;/option&gt;
        &lt;option value="108"&gt;Fiat&lt;/option&gt;
        &lt;option value="94"&gt;Ford&lt;/option&gt;
        &lt;option value="111"&gt;Honda&lt;/option&gt;
        &lt;option value="340"&gt;Hummer&lt;/option&gt;
        &lt;option value="112"&gt;Hyundai&lt;/option&gt;
        &lt;option value="365"&gt;Infiniti&lt;/option&gt;
        &lt;option value="113"&gt;Jaguar&lt;/option&gt;
        &lt;option value="114"&gt;Jeep&lt;/option&gt;
        &lt;option value="150"&gt;Kia&lt;/option&gt;
        &lt;option value="115"&gt;Lada&lt;/option&gt;
        &lt;option value="116"&gt;Lamborghini&lt;/option&gt;
        &lt;option value="117"&gt;Lancia&lt;/option&gt;
        &lt;option value="168"&gt;Land Rover&lt;/option&gt;
        &lt;option value="432"&gt;Landwind&lt;/option&gt;
        &lt;option value="118"&gt;Lexus&lt;/option&gt;
        &lt;option value="119"&gt;Lotus&lt;/option&gt;
        &lt;option value="120"&gt;Maserati&lt;/option&gt;
        &lt;option value="330"&gt;Maybach&lt;/option&gt;
        &lt;option value="121"&gt;Mazda&lt;/option&gt;
        &lt;option value="122"&gt;Mercedes-Benz&lt;/option&gt;
        &lt;option value="304"&gt;Mini&lt;/option&gt;
        &lt;option value="124"&gt;Mitsubishi&lt;/option&gt;
        &lt;option value="126"&gt;Morgan&lt;/option&gt;
        &lt;option value="127"&gt;Nissan&lt;/option&gt;
        &lt;option value="128"&gt;Opel&lt;/option&gt;
        &lt;option value="130"&gt;Peugeot&lt;/option&gt;
        &lt;option value="132"&gt;Porsche&lt;/option&gt;
        &lt;option value="134"&gt;Renault&lt;/option&gt;
        &lt;option value="135"&gt;Rolls-Royce&lt;/option&gt;
        &lt;option value="138"&gt;Saab&lt;/option&gt;
        &lt;option value="139"&gt;Seat&lt;/option&gt;
        &lt;option value="140"&gt;Skoda&lt;/option&gt;
        &lt;option value="226"&gt;smart&lt;/option&gt;
        &lt;option value="343"&gt;Spyker&lt;/option&gt;
        &lt;option value="210"&gt;SsangYong&lt;/option&gt;
        &lt;option value="141"&gt;Subaru&lt;/option&gt;
        &lt;option value="142"&gt;Suzuki&lt;/option&gt;
        &lt;option value="417"&gt;Think&lt;/option&gt;
        &lt;option value="144"&gt;Toyota&lt;/option&gt;
        &lt;option value="147"&gt;Volkswagen&lt;/option&gt;
        &lt;option value="145"&gt;Volvo&lt;/option&gt;

    &lt;/select&gt;
</code></pre>

<p>, I am wondering is there anyway's i can add the above string text to <strong>driver.page_source</strong> , So that i can iterate over the options values using <strong>driver</strong> properties ?</p>

<pre><code>from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import Select


driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://autotelexpro.nl/LoginPage.aspx')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_txtVestigingsnummer"]').send_keys('3783')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_txtGebruikersnaam"]').send_keys('Frank')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_Password"]').send_keys('msnauto2016')
driver.find_element(By.XPATH, value ='//*[@id="ctl00_cp_LogOnView_LogOn_btnLogin"]').click()
time.sleep(10)


currentselection = driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlVoertuigType']")
select = Select(currentselection)
select.select_by_visible_text("Motoren")
time.sleep(5)

try:
    x=driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwdag']")
    select = Select(x)
    select.select_by_visible_text("1")

    y=driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwmaand']")
    select = Select(y)
    select.select_by_visible_text("1")

    z=driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwjaar']")
    select = Select(z)
    select.select_by_visible_text("2017")
    time.sleep(5)


    car = driver.find_element_by_css_selector("#ctl00_cp_ucSearch_Manual_ddlMerk")
    select = Select(car)
    select.select_by_visible_text("BTC")
except:
    print "Not able to select"
</code></pre>

<p>this code will help.
See better way is explicit wait but for temp solution i have used time.sleep()</p>

<p>Update: If you want to get the option from car dropdown this is a method which can be used:</p>

<pre><code>def getallcarlist():
    currentselection = driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlVoertuigType']")
    select = Select(currentselection)
    select.select_by_visible_text("Motoren")
    time.sleep(5)

    x = driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwdag']")
    select = Select(x)
    select.select_by_visible_text("1")

    y = driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwmaand']")
    select = Select(y)
    select.select_by_visible_text("1")

    z = driver.find_element_by_xpath(".//*[@id='ctl00_cp_ucSearch_Manual_ddlBouwjaar']")
    select = Select(z)
    select.select_by_visible_text("2017")
    time.sleep(5)

    car = driver.find_element_by_css_selector("#ctl00_cp_ucSearch_Manual_ddlMerk")
    carlist =[]
    for option in car.find_elements_by_tag_name('option'):
        carlist.append((option.text).encode('utf8'))
    return carlist
</code></pre>

<p>this is way to call it </p>

<pre><code>listcar= getallcarlist()
for c in listcar:
    print c
</code></pre>

<p>the output will be:</p>

<pre><code>- Kies merk -
AGM
AJP
Aprilia
Benelli
Beta
BMW
BTC
Bullit
Derbi
Ducati
Energica
Gilera
Harley Davidson
Hesketh
Honda
Husqvarna
Hyosung
Indian
Kawasaki
KTM
Kymco
Longjia
Mash
Morgan
Mors
Moto Guzzi
MV Agusta
Nimoto
Ossa
Peugeot
Piaggio
Quadro
Razzo
Renault
Royal Enfield
Sachs
Scomadi
Suzuki
SWM
SYM
Triumph
Turbho
Vespa
Victory
Volta Motorbikes
Yamaha
Yiben
Zero Motorcycles
</code></pre>

python python-2.7 selenium post selenium-chromedriver