How to select element using XPATH syntax on Selenium for Python?

前端 未结 2 456
醉酒成梦
醉酒成梦 2020-12-09 02:42

consider following HTML:

abc

I want t

相关标签:
2条回答
  • 2020-12-09 02:55

    HTML

    <div id='a'>
      <div>
        <a class='click'>abc</a>
      </div>
    </div>
    

    You could use the XPATH as :

    //div[@id='a']//a[@class='click']
    

    output

    <a class="click">abc</a>
    

    That said your Python code should be as :

    driver.find_element_by_xpath("//div[@id='a']//a[@class='click']")
    
    0 讨论(0)
  • 2020-12-09 03:04

    Check this blog by Martin Thoma. I tested the below code on MacOS Mojave and it worked as specified.

    > def get_browser():
    >     """Get the browser (a "driver")."""
    >     # find the path with 'which chromedriver'
    >     path_to_chromedriver = ('/home/moose/GitHub/algorithms/scraping/'
    >                             'venv/bin/chromedriver')
    >     download_dir = "/home/moose/selenium-download/"
    >     print("Is directory: {}".format(os.path.isdir(download_dir)))
    > 
    >     from selenium.webdriver.chrome.options import Options
    >     chrome_options = Options()
    >     chrome_options.add_experimental_option('prefs', {
    >         "plugins.plugins_list": [{"enabled": False,
    >                                   "name": "Chrome PDF Viewer"}],
    >         "download": {
    >             "prompt_for_download": False,
    >             "default_directory": download_dir
    >         }
    >     })
    > 
    >     browser = webdriver.Chrome(path_to_chromedriver,
    >                                chrome_options=chrome_options)
    >     return browser
    
    0 讨论(0)
提交回复
热议问题