简体   繁体   English

Selenium Python:无法通过 id 获取元素,其中 id = principal

[英]Selenium Python : Unable to get element by id where id = principal

For my daily work, I'm trying to create a Python Script which is able to fill in different forms, from different websites.对于我的日常工作,我正在尝试创建一个 Python 脚本,该脚本能够填写来自不同网站的不同 forms。

Here is the thing, for some kinds of website, I'm not able to catch the form elements which Selenium.事情是这样的,对于某些类型的网站,我无法捕捉到 Selenium 的表单元素。 Eg in this website: https://econnect.bpcl.in/selfservice-ext/pub/login.html Where I inspect the page with Chrome, the input "User id" box has the id "principal", but Selenium is not able to get it. Eg in this website: https://econnect.bpcl.in/selfservice-ext/pub/login.html Where I inspect the page with Chrome, the input "User id" box has the id "principal", but Selenium is not能够得到它。 And when I display the html code of this page, the form looks like being included from another page or something.当我显示此页面的 html 代码时,表单看起来像是从另一个页面或其他内容中包含的。

I tried to getelementbyid, byname, bycssselector, etc. I also tried to wait for the page to be entirely loaded by using WebDriverWait(driver, 5), but it still do not work.我尝试 getelementbyid、byname、bycssselector 等。我还尝试使用 WebDriverWait(driver, 5) 等待页面完全加载,但它仍然不起作用。

i also tryied driver.execute_async_script and driver.execute_script我也尝试了 driver.execute_async_script 和 driver.execute_script

Do you have any solutions or suggestions?您有什么解决方案或建议吗?

PS: Even with javascript, I'm not able to get this element by id PS:即使使用 javascript,我也无法通过 id 获取此元素

Thanks谢谢

You can try to use x-path, I'm using a chrome extension: https://chrome.google.com/webstore/detail/xpath-finder/ihnknokegkbpmofmafnkoadfjkhlogph?hl=en您可以尝试使用 x-path,我使用的是 chrome 扩展: https://chrome.google.com/webstore/detail/xpath-finder/ihnknokegkbpmofmafnkoadfjkhlogph?hl=en

To locate the element you want to fill定位要填充的元素

And then you can use driver.find_element_by_xpath("X-PATH").sendkeys("...")然后你可以使用driver.find_element_by_xpath("X-PATH").sendkeys("...")
If you don't want to download a chrome extension you can try to go the HTML script, but I doubt it would work because you said it wasn't working for you...如果您不想下载 chrome 扩展程序,您可以尝试 go HTML 脚本,但我怀疑它会起作用,因为您说它不适合您...

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

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