似乎无法使用Selenium和python单击元素

时间:2019-07-21 19:42:31

标签: python-3.x selenium xpath webdriverwait xpath-1.0

我正在尝试单击“最有用的第一”下拉列表,并将其更改为android playstore应用程序的最新评论。

https://play.google.com/store/apps/details?id=com.nintendo.zama&showAllReviews=true

还通过找到类和xpath尝试了以下方法之类的多种方法,但没有任何效果。

new_comments = driver.find_element_by_link_text('"Most helpful first"').click()

5 个答案:

答案 0 :(得分:1)

您应该对链接文本使用find_element_by_link_text方法。

例如,考虑此页面源:

<html>
<body>
  <p>Are you sure you want to do this?</p>
  <a href="continue.html">Continue</a>
  <a href="cancel.html">Cancel</a>
</body>
<html>

因此,如果您想按链接文本单击:

continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')

,对于您来说,“最有帮助的第一”文本不是<span>标记中的链接文本。

因此您可以使用driver.find_element_by_css_selector方法或driver.find_element_by_xpath方法:

CSS

driver.find_element_by_css_selector('.KKjvXb > span');

XPATH

driver.find_element_by_xpath("//span[text() = 'Most helpful first']");

答案 1 :(得分:0)

这不是此元素,您应该使用其他元素并使用send_keys,要检查它是否需要按F12并注意突出显示“最有帮助的第一”字段的元素。这是您已经在寻找的元素的一部分,对我来说是这样的:

from selenium.webdriver.common.keys import Keys
driver.find_element_by_xpath('//*[@class="MocG8c UFSXYb LMgvRb KKjvXb"]').send_keys(Keys.ARROW_DOWN, Keys.ARROW_DOWN)

然后您看到了另一个列表,您需要从列表中找到合适的元素:

driver.find_elements_by_xpath('//div[@jsname="wQNmvb"]')[-3].click()

我使用索引-3,因为有6个具有相同xpath的元素。

答案 2 :(得分:0)

要在文本为最有帮助的click(),然后在文本为最新的元素上click()为所需的element_to_be_clickable()诱导 WebDriverWait ,您可以使用以下Locator Strategy

  • 使用XPATH

    driver.get('https://play.google.com/store/apps/details?id=com.nintendo.zama&showAllReviews=true')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[text()='Reviews']//following::span[text()='Most helpful first'][1]"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//h2[text()='Reviews']//following::span[text()='Most helpful first'][1]//following::span[text()='Newest']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

浏览器快照:

play_google_com

答案 3 :(得分:0)

在浏览器控制台中尝试一下,然后使用javascript executor执行该操作:

$ x(“(// span [text()='最有帮助的第一人'])[1]”)[0] .click();

答案 4 :(得分:-1)

我设法做到了。

dropdown = driver.find_element_by_xpath('//*[@id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/c-wiz/div[1]/div/div[1]/div[2]').click()
sleep(2)
new_comments = driver.find_element_by_xpath('//*[@id="fcxH9b"]/div[4]/c-wiz/div/div[2]/div/div[1]/div/div/div[1]/div[2]/c-wiz/div[1]/div/div[2]/div[1]').click()