如何使用硒获取下一个兄弟xpath?

时间:2018-10-16 09:39:45

标签: python-3.x selenium selenium-webdriver xpath webdriver

我在尝试下载csv文件的地方有以下代码。

chromedriver = "/usr/lib/chromium-browser/chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#aggregated-data')
time.sleep(5)
driver.find_element_by_xpath("//div[@class='dashboard-tab-content-options-state']//div[@class='dashboard-tab-content-options-item' and text()='SA']").click()
button = driver.find_element_by_xpath("//div[@class='dashboard-tab-content-download-btn' and text()= 'Download Historic Data as .csv']")
button.click() 

选择将SA提交到第一个xpath的enter image description here

但是我想从下面的选项中选择SA字段,如下所示。 enter image description here

html代码如下:-

<div id="dashADF" class="dashboard-tab-content" style="display: block;">
            <div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-options">
        <div class="dashboard-tab-content-text-container">
            <div class="dashboard-tab-content-title">Aggregated Price and Demand Data - Current Month</div>
        </div>
        <div class="dashboard-tab-content-options-state">
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item active">QLD</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">NSW</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">VIC</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">TAS</div>
            <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-options-item">SA</div>
        </div>
    </div>
</div>
<div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-download">
        <div data-tabname="dashADF" data-current="true" class="dashboard-tab-content-download-btn">Download Current Month</div>
    </div>
</div><hr>
<div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-options">
        <div class="dashboard-tab-content-text-container">
        </div>
        <div>
            <div class="dashboard-tab-content-options-state">
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item active">QLD</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">NSW</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">VIC</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">TAS</div>
                <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-options-item">SA</div>
            </div>
            <div class="dashboard-tab-content-options-time">
            </div>
        </div>
    </div>
</div>
<div class="dashboard-tab-content-row">
    <div class="dashboard-tab-content-download">
        <div data-tabname="dashADF" data-current="false" class="dashboard-tab-content-download-btn">Download Historic Data as .csv</div>
    </div>
</div>
        </div>

3 个答案:

答案 0 :(得分:0)

我会使用此XPath

val printEntries =
  cata (fn (entry, ()) => print (name entry ^ "\n")) ()

这将为您提供您想要的那个。您甚至可以将其放入函数中,并传入节名称和按钮文本,并获得所需的任意组合。

//div[.='Aggregated Price and Demand Data - Historical']/following::div[.='SA']

然后称呼它

def click_button_in_section(section_name, button_text)
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[.='{}']/following::div[.='{}']".format(section_name, button_text)))).click()

...等等

答案 1 :(得分:-1)

这两个元素看起来相同,但标签内包含相同的文本, 我希望这可以尝试尝试,

driver.find_element_by_xpath("//div[@data-tabname='dashADF' and @data-current='false' and  text()='SA']");

driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='TAS'])[2]/following::div[1]");

driver.find_element_by_xpath("(.//*[normalize-space(text()) and normalize-space(.)='Download Historic Data as .csv'])[1]/preceding::div[2]");

driver.find_element_by_xpath("//div[@id='dashADF']/div[3]/div/div[2]/div/div[5]");

driver.find_element_by_xpath("//*[@id='dashADF']/div[3]/div/div[2]/div[1]/div[5]");

答案 2 :(得分:-1)

总价格和需求数据-历史部分中的 SA 标签上的click(),您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.aemo.com.au/Electricity/National-Electricity-Market-NEM/Data-dashboard#aggregated-data')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='dashboard-tab-content' and @id='dashADF']//following::div[@class='dashboard-tab-content-options-item' and text()='SA'][2]"))).click()
    
  • 浏览器快照:

second_SA