简体   繁体   English

python 中的 selenium。 试图 select 元素

[英]selenium in python. trying to select element

UPDATE: I found out what caused the issue in my original post, but don't know how to solve.更新:我在原始帖子中发现了导致问题的原因,但不知道如何解决。 Here is some more of my code.这是我的更多代码。 I first did a search, after which my page in firefox jumped to www.flickr.com/searc/?text=volleybal.我首先进行了搜索,然后我在 firefox 中的页面跳转到 www.flickr.com/searc/?text=volleybal。 but apparently my browser object still sits on www.flickr.com.但显然我的浏览器 object 仍然位于 www.flickr.com 上。 How can I update it?我该如何更新它?

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.flickr.com')
s = browser.find_element_by_id('search-field')
s.send_keys('volleyball')
s.submit()
s = browser.find_element_by_class_name("style-button")
#s = browser.find_element_by_xpath("//li[@data-style-value = 'minimalism']")
s.click()

original post:原帖:

I'm experimenting with selenium, and I want to select the following element:我正在试验 selenium,我想 select 以下元素:

<li class="style-button minimalist" data-style-value="minimalism" data-tooltip-title="Minimalist" tabindex="0" role="button" aria-label="Minimalist">

I have tried the following, but none of them work:我尝试了以下方法,但它们都不起作用:

s = browser.find_element_by_class_name("style-button minimalist")

s = browser.find_element_by_class_name("style-button.minimalist")

s = browser.find_element_by_css_selector("li.style-button.minimalist")

s = browser.find_element_by_css_selector(".style-button.minimalist")

How can I select this element?我怎样才能 select 这个元素?

You could try an XPath here:您可以在这里尝试 XPath:

s = browser.find_element_by_xpath("//li[contains(@class, 'style-button')]")

Hope this helps a bit.希望这个对你有帮助。

Solved.解决了。 Added browser.refresh() after s.submit()s.submit() browser.refresh() )

The best option is to use absolute xpath .最好的选择是使用绝对 xpath It for that button will be:该按钮将是:

/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[2]/ul[1]/li[3]

Moreover, I found that sometimes the page is not loaded and the selenium searches for the element, which results in error.此外,我发现有时页面没有加载,selenium搜索元素,导致错误。 You can wait for the element to appear.您可以等待元素出现。 You can find more information here .您可以在此处找到更多信息。

With the combination of these, your code will be:结合这些,您的代码将是:

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


browser = webdriver.Firefox()
browser.get('https://www.flickr.com/')
s = browser.find_element_by_id('search-field')
s.send_keys('volleyball')
s.submit()

s = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/div[1]/div[1]/div[2]/ul[1]/li[3]")))
s.click()

your code works perfectly您的代码完美运行

try download last version of geckodriver for your using OS and set your executable_path尝试为您使用的操作系统下载最新版本的geckodriver并设置您的executable_path

it should like this它应该像这样

browser = webdriver.Firefox(executable_path="./geckodriver")

and try again run your improved code并再次尝试运行您改进的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM