英文:
How to click a Button which is located under a Shadow-Root
问题
嘿,我正在尝试点击位于Shadow-Root中的按钮,我对这个主题还很陌生,这是我第一次使用Shadow Roots。
一些建议:我在Visual Studio Code上使用Mac,在C#中使用Selenium和Chromedriver。在Shadow-Root中打开Chrome,点击和填写文本框都能正常工作。
提前感谢你的帮助!!
我还尝试过使用driver.executescript
,但我无法让它起作用,它说执行脚本的某些内容丢失(我猜是初学者的问题)。
结果应该是点击“接受”(Akzeptieren)按钮。
英文:
Hey so I am trying to click a Button which is located in a Shadow-Root, I am new to this topic and this is my first time working with Shadow Roots.
Some Information: I am working in Visual Studio Code on a Mac, C# using Selenium and the Chromedriver. Opening Chrome and clicking and filling Textbooks out of the Shadow-Root works.
Thanks in Advance for your Help!!
I also tried to use the driver.executescript but I can't manage to get it working, it says that something with the executescript is missing (beginner problem I guess).
Result should be that the Accept (Akzeptieren) Button will be clicked.
答案1
得分: 0
尝试这个,如果你使用的是 Selenium 版本 > 4,它支持原生影子 DOM:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.all4golf.de/?gclid=CjwKCAjw67ajBhAVEiwA2g_jEP_x3w6PJibWICIaE61LtLDJO_EEy85mgKRseNvZIAJpsA4Qw1yQMBoCOpkQAvD_BwE");
IWebElement root = driver.FindElement(By.Id("usercentrics-root"));
ISearchContext shadow = root.GetShadowRoot();
IWebElement accept = shadow.FindElement(By.CssSelector(".sc-eDvSVe.eDuWYU"));
accept.Click();
// 退出驱动程序
driver.Quit();
如果你需要更多帮助,请随时告诉我。
英文:
try this, If you are using selenium version > 4 , it has native shadow dom support
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.all4golf.de/?gclid=CjwKCAjw67ajBhAVEiwA2g_jEP_x3w6PJibWICIaE61LtLDJO_EEy85mgKRseNvZIAJpsA4Qw1yQMBoCOpkQAvD_BwE");
IWebElement root = driver.FindElement(By.Id("usercentrics-root"));
ISearchContext shadow = root.GetShadowRoot();
IWebElement accept = shadow.FindElement(By.CssSelector(".sc-eDvSVe.eDuWYU"));
accept.Click();
// Quit the driver
driver.Quit();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论