如果您想了解通过find_element_by_class_name单击按钮不起作用pythonseleniumwebdriver不起作用的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于c
如果您想了解通过find_element_by_class_name单击按钮不起作用python selenium webdriver不起作用的相关知识,那么本文是一篇不可错过的文章,我们将为您提供关于c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、find_element_by_xpath 在无头模式下不起作用 python、If 语句在 Selenium Webdriver 中不起作用、java – 为什么拖放在Selenium Webdriver中不起作用?的有价值的信息。
本文目录一览:- 通过find_element_by_class_name单击按钮不起作用python selenium webdriver不起作用
- c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用
- find_element_by_xpath 在无头模式下不起作用 python
- If 语句在 Selenium Webdriver 中不起作用
- java – 为什么拖放在Selenium Webdriver中不起作用?
通过find_element_by_class_name单击按钮不起作用python selenium webdriver不起作用
我正在尝试使用Python和Selenium在LinkedIn上添加联系人。我试图通过在“网络”选项卡(https://www.linkedin.com/mynetwork)中添加LinkedIn提出的联系建议来实现此目的,该选项具有无限滚动功能。
基本上,我希望脚本找到每个建议的配置文件旁边的“连接”按钮,单击该按钮,然后重复执行直到出现错误为止,从而脚本应向下滚动以加载更多“连接”按钮以进行重申。
我发现找到按钮元素的最好方法是使用find_element_by_class_name(),因为所有连接按钮都具有相同的类。我也尝试过使用CSS和Xpath定位元素,但没有成功。
问题:脚本能够单击第一个“连接”按钮,但之后没有一个。我已经尝试了许多实现想法(通过Xpath,CSS进行定位,并使用一系列按钮单击),但是似乎没有一个可行。以下是脚本的相关部分。
while True: try: driver.find_element_by_class_name("mn-person-card__person-btn-ext.button-secondary-medium").click() time.sleep(1) except: pass print("trying to scroll") driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") time.sleep(1)
有任何想法吗?在我看来,似乎代码应该可以工作,并且似乎还有其他阻碍成功的因素。可能是错误或类似错误。可能会提到我对所有这些都还很陌生,这是我尝试制作的第一个操作浏览器的脚本。
我正在使用Firefox驱动程序。完整的脚本可以在这里找到:http :
//pastebin.com/qtdNsRtz
提前致谢!
答案1
小编典典您应该使用find_elements
查找具有相同类的所有元素的方法来尝试获取所有元素:
elements = driver.find_elements_by_class_name("mn-person-card__person-btn-ext.button-secondary-medium")
然后使用for循环单击它们中的每个。例如:
for e in elements: e.click()
c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用
NoSuchElementException was unhandled by user code An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code Additional information: Unable to locate element: {"method":"css selector","selector":"#assessment-472 .status-PushedToSEAS"}
它就在这里抛出:
Thread.Sleep(600); wait.Until(drv => drv.FindElement(By.CssSelector("#" + assessmentQueueId + " .status-PushedToSEAS")));
我可以看到浏览器打开,我看到它到达那一点,我可以检查元素以查看元素是否存在.它的id完全正确.
我们有这个问题很多,到目前为止解决方案是在它前面抛出Thread.Sleep(600)(或类似的时间). wait.Until()的重点是不必这样做,这使我们的测试套件变得非常长.另外,正如您在上面的示例中所看到的,有时我们甚至在将Thread.Sleep()放在它前面之后也会遇到问题,我们必须延长时间.
为什么.NET Selenium的WebDriver.Until()不起作用,并且有一种不同的方式来做同样的事情而不需要等待一段时间?同样,问题是间歇性的,这意味着它有时只会发生,它可能发生在任何数量的wait.Until()语句中,而不仅仅是显示的那个!
编辑:
这是一个类变量.
私人WebDriver等等;
它实例化如下:
this.wait = new webdriverwait(driver,TimeSpan.FromSeconds(10));
解决方法
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
为了给我这个想法,我在一个不同的问题上充分赞扬了this answer.
find_element_by_xpath 在无头模式下不起作用 python
browser_locale = 'fr'
options = webdriver.ChromeOptions()
options.add_argument("--lang={}".format(browser_locale))
#options.headless = True
options.add_argument("--headless")
browser = webdriver.Chrome(options=options)
browser.get('https://www.ibm.com/demos/live/tts-demo/self-service/home')
browser.find_element_by_xpath('//*[@aria-labelledby="downshift-0-label downshift-0-toggle-button"]').click()
browser.find_element_by_xpath(
'//*[@and contains(text(),"German")]').click()
browser.find_element_by_xpath(
'//*[@aria-labelledby="downshift-0-label downshift-0-toggle-button"]').click()
browser.find_element_by_xpath(
'//*[@and contains(text(),"French")]').click()
voice = browser.find_element_by_xpath(
'//*[@id="downshift-2-toggle-button"]/span')
voice.click()
browser.find_element_by_xpath("//div[.='Renee']").click()
search = browser.find_element_by_xpath('//*[@id="text-area"]')
search.clear()
search.send_keys("text here")
只需再次选择语言以启用下拉菜单,在无头模式下它显示为禁用,截图并检查
If 语句在 Selenium Webdriver 中不起作用
如何解决If 语句在 Selenium Webdriver 中不起作用?
我总是对 selenium 中的 if 语句有疑问。大多数时候,他们做的事情与他们应该做的相反。
我故意输入了错误的电子邮件地址,因此付款将失败。你哎呀!显示在网页上,这是正确的响应,但是我的测试返回失败。当我输入正确的电子邮件时,xpath 是正确的。
请有人看一下并告诉我我哪里出错了。
@Test(dependsOnMethods = { "WithdrawalButtonOnNavBar" })
public void WithdrawWinnings() throws InterruptedException {
Thread.sleep(500);
driver.findElement(By.cssSelector("input[name=''email'']")).sendKeys(skrillAccount);
Thread.sleep(500);
driver.findElement(By.xpath("//p[contains(text(),''Continue'')]")).click();
Thread.sleep(500);
driver.findElement(By.cssSelector("input[name=''amount'']")).sendKeys("2");
Thread.sleep(500);
driver.findElement(By.xpath("//*[@id=\"__next\"]/div/div[1]/div[2]/div/div[3]/div/div/div")).click();
Thread.sleep(5000);
WebElement Success = driver.findElement(By.xpath("//p[contains(text(),''Success!'')]"));
WebElement Oops = driver.findElement(By.xpath("//p[contains(text(),''Oops!'')]"));
if (Success.isdisplayed()) {
System.out.println("Payment was successful");
} else if(Oops.isdisplayed()) {
System.out.println("Payment was unsuccessful");
}
;
}
org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{"method":"xpath","selector":"//p[contains(text(),''Success!'')] "} (会话信息:chrome=90.0.4430.85) 有关此错误的文档,请访问:https://selenium.dev/exceptions/#no_such_element
解决方法
这与 if
语句无关。查找“成功”元素时抛出异常。显式等待应该会有所帮助:
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement Success = wait.Until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//p[contains(.,''Success!'')]"));
if (Success.isDisplayed())
我还发现 contains(.,''...'')
比 contains(text(),''...'')
更宽容。
java – 为什么拖放在Selenium Webdriver中不起作用?
WebElement sourceelement = driver.findElement(By.cssSelector("XXX")); WebElement destelement = driver.findElement(By.cssSelector("YYY"));
代码1: –
Actions builder = new Actions( _controls.getDriver()); builder.dragAndDrop(sourceelement,destelement);
代码2: –
Actions builder = new Actions(_controls.getDriver()); Action dragAndDrop = builder.clickAndHold(sourceelement).movetoElement(destelement).release(destelement).build(); Thread.sleep(2000); dragAndDrop.perform()
CODE3: –
Point coordinates1 = sourceelement.getLocation(); Point coordinates2 = destelement.getLocation(); Robot robot = new Robot(); robot.mouseMove(coordinates1.getX(),coordinates1.getY()); robot.mousepress(InputEvent.BUTTON1_MASK); robot.mouseMove(coordinates2.getX(),coordinates2.getY()); robot.mouseRelease(InputEvent.BUTTON1_MASK); Thread.sleep(2000);
码4: –
final String java_script = "var src=arguments[0],tgt=arguments[1];var dataTransfer={dropEffe" + "ct:'',effectAllowed:'all',files:[],items:{},types:[],setData:fun" + "ction(format,data){this.items[format]=data;this.types.append(for" + "mat);},getData:function(format){return this.items[format];},clea" + "rData:function(format){}};var emit=function(event,target){var ev" + "t=document.createEvent('Event');evt.initEvent(event,true,false);" + "evt.dataTransfer=dataTransfer;target.dispatchEvent(evt);};emit('" + "dragstart',src);emit('dragenter',tgt);emit('dragover',tgt);emit(" + "'drop',tgt);emit('dragend',src);"; ((JavascriptExecutor)_controls.getDriver()).executeScript(java_script,sourceelement,destelement); Thread.sleep(2000);
以上代码都没有为我工作.所有上述运行没有任何错误,但拖放并没有发生在应用程序中.任何人有任何其他解决方案?谢谢.
解决方法
不调用perform()方法,
它应该是
Actions builder = new Actions( _controls.getDriver());
builder.dragAndDrop(sourceelement,destelement).perform();
在你的代码2:我不认为你需要调用release()
请在发布之前搜索similar questions.
关于通过find_element_by_class_name单击按钮不起作用python selenium webdriver不起作用的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、find_element_by_xpath 在无头模式下不起作用 python、If 语句在 Selenium Webdriver 中不起作用、java – 为什么拖放在Selenium Webdriver中不起作用?的相关知识,请在本站寻找。
本文标签: