我正在寻找有关代码的帮助。
我正在尝试从网站上打印出一个元素,但是从尝试的方法中我得到了不同的错误消息,但是它们都不起作用。
我尝试将class_name与以下代码一起使用:
ssh my_remote_ssh_alias -F /home/host_username/.ssh/config 'make -C /remote/path/to/project'
这返回了以下错误消息:
from selenium import webdriver
browser = webdriver.Chrome('/home/cena/Downloads/chromedriver')
poeisz_url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
poeisz_classname = "full-product full-product--price__for"
browser.get(poeisz_url)
elem = browser.find_element_by_class_name(poeisz_classname)
print(elem.text)
所以我尝试使用xpath和css_selector来解决它:
Traceback (most recent call last):
File "/home/cena/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 9, in <module>
elem = browser.find_element_by_class_name(poeisz_classname)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-50-generic x86_64)
from selenium import webdriver
browser = webdriver.Chrome('/home/cena/Downloads/chromedriver')
poeisz_url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
poeisz_classname = "full-product full-product--price__for"
browser.get(poeisz_url)
elem = browser.find_element_by_xpath("//*[@class='" + poeisz_classname + "']")
print(elem.text)
他们按顺序返回了以下错误:
from selenium import webdriver
browser = webdriver.Chrome('/home/cena/Downloads/chromedriver')
poeisz_url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
poeisz_classname = "full-product full-product--price__for"
browser.get(poeisz_url)
elem = browser.find_element_by_css_selector("*[class^='" + poeisz_classname + "']")
print(elem.text)
Traceback (most recent call last):
File "/home/cena/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 9, in <module>
elem = browser.find_element_by_xpath("//*[@class='" + poeisz_classname + "']")
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@class='full-product full-product--price__for']"}
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Linux 4.15.0-50-generic x86_64)
我对编程还很陌生,所以很抱歉这太愚蠢了。预先感谢您的帮助!
答案 0 :(得分:1)
您应该使用WebDriverWait
:
例如XPath:
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
driver = webdriver.Chrome('/home/cena/Downloads/chromedriver')
url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
driver.get(url)
my_el = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[@id="products"]//*[@class="full-product full-product--price__for"]')))
print(my_el.text)
输出为:
0 99
使用WebDriverWait
的更好方法是将其用作var:
wait = WebDriverWait(driver, 20)
my_el = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="products"]//*[@class="full-product full-product--price__for"]')))
根据@DebanjanB的评论,您可能想使用visibility_of_element_located()
而不是presence_of_element_located
。
希望这对您有帮助!
答案 1 :(得分:-1)
第一个错误消息中说明了您遇到的问题。
无效的选择器:不允许使用复合类名称
onclick
使用一个单个类名作为参数,但是“ full-product full-product--price__for”包含两个类名(注意:类名用空格分隔,因此这里有两个...“完整产品”和“完整产品-价格__for”。
您可以将定位器转换为CSS选择器find_element_by_class_name()
,它应该可以正常工作。
要了解有关CSS选择器的更多信息,请参见下面的链接。
Selenium Tips: CSS Selectors
WC3 reference, Selectors Level 4
Taming Advanced CSS Selectors