Webdriver cannot find xpath of "new loaded element"

Viewed 87

Currently I am trying to scrape some information off the web, but I kinda ran into a dead end.

So in order to get the data I want, I need to select the right currency off of a list. I click the link to change the currency and via java script the site gets another chunk of html that contains the list and opens an overlay, where I can select the right currency.

I tried to locate the div in which the list is stored:

driver.find_elements_by_xpath("//html/body/div[19]/div[2]/div/div/div[2]/form/div[2]")

I tried the list itself but I wasnt able to locate the element.

Then I wanted to see if something is found and using the following:

len(driver.find_elements_by_xpath("//html/body/div[19]/div[2]/div/div/div[2]/form/div[2]"))

the answer 0 was provided, so my webdriver wasnt able to locate the newly added chunk of html.

This is the newly added html:

<div class="module">
    <div class="modal_mask" style="opacity: 0.5;"/>
        <div class="modal_scroller">
            <div class="modal_container" style="margin-top: 164.5px;">
                <div class="modal_ship" style="margin: 0px auto;">
                    <div class="modal_title">
                        Address  Currency
                        <i onclick="modal_remove();">×</i>
                    </div>
                    <div class="modal_ship_con">
                        <h3>Please select your shipping destination & currency</h3>
                        <p>Price may differ based on your Shipping destination.</p>
                        <form action="#" method="get">
                            <input value="81" name="country_sel" type="hidden"/>
                            <input value="USD" name="currency_sel" type="hidden"/>
                            <div class="currency">
                                <b>Currency:</b>
                                <div class="currency_list">
                                    <div class="active">
                                        <i class="arrow_a">
                                            <i/>
                                            </i>
                                        <span>
                                            USD
                                            <u>US$</u>
                                        </span>
                                    </div>
                                    <div class="currency_box" style="display: none;">
                                        <ul>
                                            <li sel="USD">
                                                USD
                                                <u>US$</u>
                                            </li>
                                            <li sel="EUR">
                                                EUR
                                                <u>€</u>
                                            </li>
                                            <li sel="GBP">
                                                GBP
                                                <u>£</u>
                                            </li>
                                            <li sel="AUD">
                                                AUD
                                                <u>AU$</u>
                                            </li>
                                            <li sel="CAD">
                                                CAD
                                                <u>CA$</u>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="submit">
                                <input onclick="saveShip(0);" value="Save" type="button"/>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

And this where I have to click:

<div class="active">
    <i class="arrow_a">
        <i/>
        </i>
    <span>
        USD
        <u>US$</u>
    </span>
</div>

So now my question: is there any possibility to get to the newly added html code? Is there something like "refresh webdriver"?

Regards

1 Answers

I think you can simply use time.sleep() before trying to capture the button you want to click.

Just use this:

    import time
    time.sleep(2) #you can keep this behind the code for clicking the button

This will make selenium wait for sometime so that the code can load and then only selenium will click it

you can keep any no. of seconds instead of 2,I just kept it for an example

Related