点击位于 Shadow-Root 下的按钮。

huangapple go评论70阅读模式
英文:

How to click a Button which is located under a Shadow-Root

问题

嘿,我正在尝试点击位于Shadow-Root中的按钮,我对这个主题还很陌生,这是我第一次使用Shadow Roots。

网站:
https://www.all4golf.de/?gclid=CjwKCAjw67ajBhAVEiwA2g_jEP_x3w6PJibWICIaE61LtLDJO_EEy85mgKRseNvZIAJpsA4Qw1yQMBoCOpkQAvD_BwE

一些建议:我在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.

The Website:
https://www.all4golf.de/?gclid=CjwKCAjw67ajBhAVEiwA2g_jEP_x3w6PJibWICIaE61LtLDJO_EEy85mgKRseNvZIAJpsA4Qw1yQMBoCOpkQAvD_BwE

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();

huangapple
  • 本文由 发表于 2023年5月24日 18:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定