首页 > 解决方案 > Xpath 在 Chrome 和 Mozilla 中不同,以识别在 Selenium Python 中工作的相同元素复选框

问题描述

复选框对于 Chrome(版本 81.0.4044.138)和 Firefox(76.0.1(64 位))有不同的 xpath,它一次只能工作一个,我收到以下错误:

============================= 测试开始================== =========== 平台 win32 -- Python 3.7.4, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 rootdir: C:\eclipse-workspace2\Hybrid-Framework 插件: allure-pytest-2.8.13 收集到 3 个项目

Testcases\test_login.py sF INTERNALERROR> Traceback(最近一次调用最后一次):INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\main.py”,第 191 行,在 wrap_session INTERNALERROR> session.exitstatus = doit(config, session) 或 0 INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\main.py",行247,在 _main INTERNALERROR> config.hook.pytest_runtestloop(session=session) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\hooks.py”,第 286 行,通话中 INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\manager.py" ,第 93 行,在 _hookexec INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\manager. py",第 87 行,在 INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37 \lib\site-packages\pluggy\callers.py",第 208 行,在 _multicall INTERNALERROR> 返回 output.get_result() INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\callers.py",第 80 行,在 get_result INTERNALERROR> raise ex1 .with_traceback(ex[2]) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\callers.py”,第 187 行,在 _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\main.py",第 272 行,在 pytest_runtestloop INTERNALERROR> 项中。 config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\hooks.py”,第 286 行,通话中 INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\manager.py" ,第 93 行,在 _hookexec INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\manager. py",第 87 行,在 INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37 \lib\site-packages\pluggy\callers.py",第 208 行,在 _multicall INTERNALERROR> 返回 output.get_result() INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\callers.py",第 80 行,在 get_result INTERNALERROR> raise ex1.with_traceback(ex[2]) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\callers.py”,第 187 行,在 _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\runner.py",第 85 行,在 pytest_runtest_protocol INTERNALERROR> runtestprotocol(item , nextitem=nextitem) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\runner.py”,第 100 行,在 runtestprotocol INTERNALERROR> reports.append(call_and_report(项目,“呼叫”,日志))内部错误>文件“C:\用户\所有者\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\runner.py”,第190行,在 call_and_report INTERNALERROR> hook.pytest_runtest_logreport(report=report) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\hooks.py",第 286 行,在称呼 INTERNALERROR> return self._hookexec(self, self.get_hookimpls(), kwargs) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\manager.py" ,第 93 行,在 _hookexec INTERNALERROR> return self._inner_hookexec(hook, methods, kwargs) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\manager. py",第 87 行,在 INTERNALERROR> firstresult=hook.spec.opts.get("firstresult") if hook.spec else False, INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37 \lib\site-packages\pluggy\callers.py",第 208 行,在 _multicall INTERNALERROR> 返回 output.get_result() INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\callers.py",第 80 行,在 get_result INTERNALERROR> raise ex1.with_traceback(ex[2]) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages\pluggy\callers.py”,第 187 行,在 _multicall INTERNALERROR> res = hook_impl.function(*args) INTERNALERROR> 文件“C:\Users\Owner.p2\pool\plugins\org.python.pydev.core_7.5.0.202001101138\pysrc_pydev_runfiles\pydev_runfiles_pytest2.py”,第 243 行,在 pytest_runtest_logreport INTERNALERROR > exc = _get_error_contents_from_report(report) INTERNALERROR> 文件“C:\Users\Owner.p2\pool\plugins\org.python.pydev.core_7.5.0.202001101138\pysrc_pydev_runfiles\pydev_runfiles_pytest2.py”,第 147 行,在 _get_error_contents_from_report INTERNALERROR> report.toterminal(tw) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest\reports.py”,第 65 行,在 toterminal INTERNALERROR> longrepr.toterminal(out) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest_code\code.py”,第 962 行,在 toterminal INTERNALERROR> element[0].toterminal(tw) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest_code\code.py",第 992 行,在toterminal INTERNALERROR> entry.toterminal(tw) INTERNALERROR> File "C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest_code\code.py",第 1080 行,在 toterminal INTERNALERROR> self. _write_entry_lines(tw) INTERNALERROR> 文件“C:\Users\Owner\AppData\Local\Programs\Python\Python37\lib\site-packages_pytest_code\code.py”,第 1062 行,在 _write_entry_lines INTERNALERROR> tw._write_source(source_lines, indents) INTERNALERROR> AttributeError: 'TerminalWriter' 对象没有属性 '_write_source'

======================== 1 失败,1 跳过 47.12s ================= ======

XPATH for CHROME: checkbox_xpath = // [@id="signupform"]/div/div[6]/div[2]/label/span XPATH FOR MOZILLA: checkbox_xpath = // [@id="signupform"]/div /div[6]/div[2]/标签/输入

我使用 OR 选项但不工作

这是HTML:

在此处输入图像描述

标签: python-3.xseleniumxpath

解决方案


您的 XPath 表达式是不同的(注意:您忘记了*两者),因为第一个选择一个span元素,而第二个选择一个input元素。但是如果仔细观察,它们共享相同的父节点(div元素)。

最好单击此 div 以勾选复选框:

//div[@class="za-tos-container"]

注意:如果您需要进一步的帮助,请发布有关您要实现的目标、数据示例和一段代码的更多详细信息。


推荐阅读