【发布时间】:2021-11-18 14:48:44
【问题描述】:
我正在尝试在 Selenium 中编写一些测试,并且由于许多元素没有 ID 或名称,因此我尝试使用 Xpath。但是,无论如何,每当我使用 Xpath 时,我都会遇到相同的错误:
TypeError: 'str' 对象不可调用
以下是我测试过的最简单的示例,它会导致相同的错误。
这是 Python 中的代码:
from selenium.webdriver.common.by import By
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('ignore-certificate-errors')
driver = webdriver.Chrome(chrome_options=options)
driver.get('cannot_share_this_link/index.php')
driver.find_element(By.XPATH("//*[@id='sign']"))
测试日期:
<html>
<head>
<title>
Comtest
</title>
<script src="comajax.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
</head>
<body>
<div>
<br>
<input type="button" id="sign" value="Sign up" onclick="window.open('signup.php','popUpWindow','height=500,width=800,left=700,top=300,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no, status=yes');">
<br>
<input type="button" id="log" value="Log in" onclick="window.open('login.php','popUpWindow','height=500,width=800,left=700,top=300,resizable=yes,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no, status=yes');">
</div>
</body>
</html>
编辑:我想指出,这个问题不是重复的,因为链接的线程显然是在寻找一个字符串作为输出,而我正在寻找一个元素。此外,两年前在那里展示的方法现在会抛出弃用警告,如果它们甚至可以工作(因为我根本无法让它工作,我无法判断)。 我已经阅读了链接的主题,在寻找解决方案的过程中我偶然发现了它大约 30 次。可能没有关于这个问题的帖子我没有读过,否则我不会发帖。
【问题讨论】: