XPath local-name() SyntaxError: The expression is not a legal expression

Viewed 27

I'm trying to web scrape a table from an iframe. In order to switch the driver to that frame I'm using driver.find_element_by_xpath, but the problem is that the path in the html code includes some namespaces that I cannot get Python to figure out using the local-name() function.

Here is the chunk of the HTML I'm using:

<xbrl:campo-captura xbrl:solo-lectura="true" xbrl:id-hecho-plantilla="ar_pros_CorporateStructure_11933a35-3932-44c0-b394-f0ebd4f722d2" 
id="8a97271e-df5c-4fbe-bedf-513ea1508bf2"><div>
    <div>
        
        <i style="cursor:pointer; float:right;margin-right:-20px;" id="d9fa20ae-c55f-4344-baf5-0112a13827b6" class="i i-arrow-down-2 botonDetalleOperacionXbrl">
        </i>
        
        <div id="abrir_nota_F2a26d5a7-2934-4ff0-86df-7a8983c05e47" style="cursor:pointer;float:right;margin-right:-20px;margin-top:20px;" data-toggle="tooltip" data-placement="right" title="Abrir nota">
            <i class="fa fa-external-link"></i>
        </div>
    </div>

    <div class="campoTextBlock">
        <div id="F2a26d5a7-2934-4ff0-86df-7a8983c05e47">
            <div class="celdaAnchoFijo textBlockLimit div-default divTextBlockMaximo" id="divAreaTextod9fa20ae-c55f-4344-baf5-0112a13827b6" style="overflow-y:hidden">
                <iframe scrolling="no" id="frame_8a97271e-df5c-4fbe-bedf-513ea1508bf2" style="width:100%;height:100%" frameborder="0"></iframe>
            </div>
        </div>
    </div>


    <div>
        
    </div>
</div></xbrl:campo-captura>

I want to get to the "iframe" using something like:

framLogin= driver.find_element_by_xpath('//[local-name()="campo-captura"][@*[local-name()="id-
hecho-plantilla" and .="ar_pros_CorporateStructure_11933a35-3932-44c0-b394-f0ebd4f722d2"]]
/div[2]/div/div/iframe')

The message I get is

Given xpath expression ... is invalid: SyntaxError: Document.evaluate: The expression is not a legal expression.

I've already looked for more information but all I have found is not for Python. I'm aware I could get to the iframe by using its id, but later on I want to make a loop to scrap the same tables in other URLs with the exact same format, and the iframe's id is not constant.

1 Answers

Your immediate syntax error can be fixed by changing

//[local-name()="campo-captura"]

to

//*[local-name()="campo-captura"]
  ^
Related