显示错误' WebElement'对象没有属性' startswith'
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
# options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")
chrome_driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options=options
)
def supplyvan_scraper():
with chrome_driver as driver:
driver.implicitly_wait(15)
URL = 'https://www.ifep.ro/justice/lawyers/lawyerspanel.aspx'
driver.get(URL)
time.sleep(3)
link=driver.find_elements(By.XPATH, "//div[@class='list-group']//a")
for links in link:
if(links.startsWith("https://www.ifep.ro/")):
print(links.get_attribute("href"))
他们向我展示了这些行中的错误,有一些不必要的链接,我想删除这些页面链接 https://www.ifep.ro/justice/lawyers/lawyerspanel.aspx :
'WebElement' object has no attribute 'startsWith'
import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
options = webdriver.ChromeOptions()
# options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1920x1080")
options.add_argument("--disable-extensions")
chrome_driver = webdriver.Chrome(
service=Service(ChromeDriverManager().install()),
options=options
)
def supplyvan_scraper():
with chrome_driver as driver:
driver.implicitly_wait(15)
URL = 'https://www.ifep.ro/justice/lawyers/lawyerspanel.aspx'
driver.get(URL)
time.sleep(3)
link=driver.find_elements(By.XPATH, "//div[@class='list-group']//a")
for links in link:
if(links.startsWith("https://www.ifep.ro/")):
print(links.get_attribute("href"))
They show me error in these line there are some unwanted link and I want to remove it these is the page link https://www.ifep.ro/justice/lawyers/lawyerspanel.aspx:
'WebElement' object has no attribute 'startsWith'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为
WebElement
不是字符串。您必须首先从WebElement
中提取文本,然后在结果文本上使用startswith
。这是完整的代码:
您可以仅使用此修改的代码:
输出:
This is because the
WebElement
is not a string. You have to first extract the text from theWebElement
and then usestartsWith
on the resulting text.Here is the complete code:
You can use this modified code only:
Output:
尝试通过部分@HREF过滤链接,您正在尝试解决XY问题。无需过滤链接 - 只需使用正确的XPath选择所需的链接:
Trying to filter links by partial @href you're trying to solve an X-Y issue. There is no need to filter links- just use correct XPath to select required links:
链接具有多个属性,目标,位置,文本...
您很可能想要文本
应该有效
Links have multiple attributes, target, location, text...
You most likely want text
should work